我已经跟随Google docs创建了一个导航抽屉,并且跟随了它在onCreate方法中调用selectItem的结构,带有一个默认项,只是用一些东西启动片段:
if (savedInstanceState == null) {
selectItem(99); //default map to start with
}
哪个电话:
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();
}
因此,在第一次启动应用程序时,这很好用,片段打开。导航抽屉也可以工作,当我单击菜单时,它会打开抽屉,其中包含要选择的菜单项列表。
然而,当我点击一个项目时,这就是它的中断点:
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);
}
}
我遇到InflateException导致致命的崩溃:当我点击一个项目时,立即错误地膨胀类片段。
因此,函数selectItem第一次工作,但是从MainActivity的onCreate()调用它,但是在onItemClick方法中调用它并且它失败并出现该错误 - 即,当我注释掉selectItem(position)时从onItemClick方法来看,没有崩溃。
更奇怪的是,当我在DrawerItemClickListener的onItemClick内的selectItem(位置)行设置断点时,它实际上似乎有效,但我无法找到实际失败的地方,因为它打开了所有
。崩溃之前我不理解的各种内置类/功能。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" />
请求的异常堆栈:
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)