0

I have followed the Google docs to create a navigation drawer and have followed the structure where it calls selectItem in the onCreate method, with a default item, just to start the fragment with something:

if (savedInstanceState == null) {
        selectItem(99); //default map to start with
    }

Which calls:

    private void selectItem(int position) {
        MyMapFragmentActivity fragMap = new MyMapFragmentActivity();

        Bundle args = new Bundle();
        args.putInt(MyMapFragmentActivity.ARG_MENU_ITEM, position);
        fragMap.setArguments(args);

        FragmentManager fragMgr = getFragmentManager();
        fragMgr.beginTransaction().replace(R.id.content_frame, fragMap).commit();
    }

So upon first starting the app, this works nicely, the fragment opens. The navigation drawer also works, when i click the menu it opens the drawer with list of menu items to select.

However, this is where it breaks, when I click an item the listener:

    private class DrawerItemClickListener implements ListView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(MainActivity.this, "Menu clicked " + position , Toast.LENGTH_LONG).show();

        selectItem(position);

        mDrawerLayout.closeDrawer(mMenuList);
    }
}

I get a fatal crash with the InflateException: Error inflating class fragment, immediately when i click an item.

So, the function selectItem works the first time, but calling it from the onCreate() of the MainActivity, however call it inside the onItemClick method and it fails with that error - ie, when I comment out the selectItem(position) from the onItemClick method, there is no crash.

Whats even more bizarre, is when I set a breakpoint on the selectItem(position) line inside onItemClick of the DrawerItemClickListener, it actually seems to work, but I can't find where it actually is failing because it opens all sorts of built-in classes/functions that I don't understand, before it crashes.

fragment_map.xml:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />

Exception stack as requested:

 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.this.empire, PID: 26454
android.view.InflateException: Binary XML file line #2: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
        at com.example.this.empire.MyMapFragmentActivity.onCreateView(MyMapFragmentActivity.java:66)
        at android.app.Fragment.performCreateView(Fragment.java:1700)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
        at android.app.BackStackRecord.run(BackStackRecord.java:684)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1453)
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5586)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id      0x7f0a0059, tag null, or parent id 0x0 with another fragment for   com.google.android.gms.maps.SupportMapFragment
        at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2164)
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
        at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:547)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
                at com.example.this.empire.MyMapFragmentActivity.onCreateView(MyMapFragmentActivity.java:66)
            at android.app.Fragment.performCreateView(Fragment.java:1700)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
            at android.app.BackStackRecord.run(BackStackRecord.java:684)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1453)
            at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5586)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)
makapaka
  • 169
  • 5
  • 16

0 Answers0