Reputation: 178
I know there is a lot about this topic around here, but I just seem to don't get to the root of my problem. The thing is that I have a Fragment in my MainActivity, and I want to replace it with other fragment when I press my settings button in the overflow of the actionbar. This works great, but if I try to press the settings button again, or go back and then try it again, my app will crash (Unfortunately MyApp has stopped) so I would like to know what is the problem. Thanks a lot if you can help me, or if you just take the time to read it, I'm new to android programming and I would like to learn a lot about it.
09-27 17:53:16.364: W/System.err(15060): android.view.InflateException: Binary XML file line #9: Error inflating class fragment
09-27 17:53:16.364: W/System.err(15060): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
09-27 17:53:16.364: W/System.err(15060): at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
09-27 17:53:16.364: W/System.err(15060): at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
09-27 17:53:16.364: W/System.err(15060): at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
09-27 17:53:16.364: W/System.err(15060): at com.example.myfirstapp.FragmentOpAdv.onCreateView(FragmentOpAdv.java:14)
09-27 17:53:16.364: W/System.err(15060): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
09-27 17:53:16.364: W/System.err(15060): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
09-27 17:53:16.364: W/System.err(15060): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
09-27 17:53:16.364: W/System.err(15060): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-27 17:53:16.364: W/System.err(15060): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
09-27 17:53:16.364: W/System.err(15060): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
09-27 17:53:16.364: W/System.err(15060): at android.os.Handler.handleCallback(Handler.java:733)
09-27 17:53:16.364: W/System.err(15060): at android.os.Handler.dispatchMessage(Handler.java:95)
09-27 17:53:16.364: W/System.err(15060): at android.os.Looper.loop(Looper.java:136)
09-27 17:53:16.364: W/System.err(15060): at android.app.ActivityThread.main(ActivityThread.java:5579)
09-27 17:53:16.364: W/System.err(15060): at java.lang.reflect.Method.invokeNative(Native Method)
09-27 17:53:16.364: W/System.err(15060): at java.lang.reflect.Method.invoke(Method.java:515)
09-27 17:53:16.364: W/System.err(15060): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
09-27 17:53:16.364: W/System.err(15060): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
09-27 17:53:16.364: W/System.err(15060): at dalvik.system.NativeStart.main(Native Method)
09-27 17:53:16.364: W/System.err(15060): Caused by: java.lang.IllegalArgumentException: Binary XML file line #9: Duplicate id 0x7f050041, tag null, or parent id 0x0 with another fragment for com.example.myfirstapp.FragmentA
09-27 17:53:16.364: W/System.err(15060): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
09-27 17:53:16.364: W/System.err(15060): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
09-27 17:53:16.364: W/System.err(15060): ... 19 more
09-27 17:53:16.364: I/System.out(15060): Binary XML file line #9: Error inflating class fragment
09-27 17:53:16.364: E/ViewRootImpl(15060): sendUserActionEvent() mView == null
Here is the stack trace, also look at my MainActivity.class
package com.example.myfirstapp;
import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
private FragmentOpAdv fragmentOpAdv;
private FragmentA fragmentA;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(findViewById(R.id.mainact)!=null)
{
if (savedInstanceState!=null)
{
return;
}
fragmentA= new FragmentA();
getSupportFragmentManager().beginTransaction().add(R.id.mainact, fragmentA).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (R.id.action_search==id)
{
openSearch();
return true;
}
if (R.id.action_settings==id)
{
openSettings();
return true;
}
else
{
return super.onOptionsItemSelected(item);
}
}
/**
* Called when the user clicks the Settings item
*/
public void openSettings() {
fragmentOpAdv = new FragmentOpAdv();
getSupportFragmentManager().beginTransaction().replace(R.id.mainact, fragmentOpAdv).addToBackStack(null).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
}
/**
* Called when the user clicks the Search item
*/
public void openSearch() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Inserte algo");
alert.setMessage("Oiga en serio");
alert.show();
}
/** Called when the user clicks the Send button */
public void sendMessage(View view)
{
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
and my fragment class
package com.example.myfirstapp;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOpAdv extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
try {
View view = inflater.inflate(R.layout.fragment_op_adv, container, false);
return view;
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
return null;
}
}
}
Here is the xml for the main activity
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainact"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:paddingTop="?attr/actionBarSize" >
Upvotes: 1
Views: 1854
Reputation: 6509
If everything is woking same as google code then please check manifest file in my case i added geo key and map key that's why exception occurs,
Note - do not add two keys in manifest file remove map key
meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key"/>
above code and add this code.
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/auto_location"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
Upvotes: 0
Reputation: 178
The problem I just found is that I cannot inflate a layout into a fragment when that layout includes a . Nested fragments are only supported when added to a fragment dynamically.
Upvotes: 1