我遇到了一个问题,我用我的应用程序的导航抽屉无法解决。导航抽屉中的一个链接是打开FragmentActivity(包含ViewPager)。其他链接是片段。现在我把它设置为Intent来启动该活动,但是我得到一个关于我的一个Fragments,MapsPage的错误。与嵌套碎片有关。我将在下面发布logcat。这是我的NavigationDrawer onClick设置:
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Fragment newFragment = new MapsPage();
FragmentManager fm = getSupportFragmentManager();
switch(i) {
case 0:
newFragment = new FragOne();
fm.popBackStack();
break;
case 1:
newFragment = new FragTwo();
fm.popBackStack();
break;
case 2:
newFragment = new FragThree();
fm.popBackStack();
break;
case 3:
Intent in = new Intent(getApplicationContext(), FragmentPagerActivity.class);
startActivity(in);
fm.popBackStack();
break;
}
fm.beginTransaction().add(R.id.main, newFragment).addToBackStack("fragback").commit();
drawerLayout.closeDrawer(rl);
}
我的问题是“案例3:”。我的应用程序的核心MapsPage在XML中有一个映射片段,在调用它时会导致异常。这是一个例外:
01-20 18:42:09.302: E/AndroidRuntime(15950): FATAL EXCEPTION: main
01-20 18:42:09.302: E/AndroidRuntime(15950): Process: com.android.package.name, PID: 15950
01-20 18:42:09.302: E/AndroidRuntime(15950): android.view.InflateException: Binary XML file line #7: Error inflating class fragment
01-20 18:42:09.302: E/AndroidRuntime(15950): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
01-20 18:42:09.302: E/AndroidRuntime(15950): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
01-20 18:42:09.302: E/AndroidRuntime(15950): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
01-20 18:42:09.302: E/AndroidRuntime(15950): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-20 18:42:09.302: E/AndroidRuntime(15950): at com.mdev.bluestorm.drinki.MapsPage.onCreateView(MapsPage.java:103)
01-20 18:42:09.302: E/AndroidRuntime(15950): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
01-20 18:42:09.302: E/AndroidRuntime(15950): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
01-20 18:42:09.302: E/AndroidRuntime(15950): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-20 18:42:09.302: E/AndroidRuntime(15950): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-20 18:42:09.302: E/AndroidRuntime(15950): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
01-20 18:42:09.302: E/AndroidRuntime(15950): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
01-20 18:42:09.302: E/AndroidRuntime(15950): at android.os.Handler.handleCallback(Handler.java:733)
01-20 18:42:09.302: E/AndroidRuntime(15950): at android.os.Handler.dispatchMessage(Handler.java:95)
01-20 18:42:09.302: E/AndroidRuntime(15950): at android.os.Looper.loop(Looper.java:136)
01-20 18:42:09.302: E/AndroidRuntime(15950): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-20 18:42:09.302: E/AndroidRuntime(15950): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 18:42:09.302: E/AndroidRuntime(15950): at java.lang.reflect.Method.invoke(Method.java:515)
01-20 18:42:09.302: E/AndroidRuntime(15950): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-20 18:42:09.302: E/AndroidRuntime(15950): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-20 18:42:09.302: E/AndroidRuntime(15950): at dalvik.system.NativeStart.main(Native Method)
01-20 18:42:09.302: E/AndroidRuntime(15950): Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f060060, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
01-20 18:42:09.302: E/AndroidRuntime(15950): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
01-20 18:42:09.302: E/AndroidRuntime(15950): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
这是关键错误:
01-20 18:42:09.302:E / AndroidRuntime(15950):引起:java.lang.IllegalArgumentException:二进制XML文件行#7:重复ID为0x7f060060,标记为null或父id为0x0另一个com.google.android.gms.maps.SupportMapFragment
的片段我能做些什么来防止这种情况发生?我需要能够从导航抽屉中打开FragmentActivity,而不会因为它与Google maps Fragment冲突而关闭。我需要MapsPage作为backstack的一部分,实际上是唯一的一个,因为你可以看到我正在为每个其他页面弹出backstack。
我真的很感激这方面的一些帮助,我对下一步做什么感到很失望。提前谢谢。
编辑:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
答案 0 :(得分:1)
您正在致电
fm.beginTransaction().add(R.id.main,newFragment).addToBackStack("fragback").commit();
在您致电startActivity()
之后