选项卡中的谷歌地图v2出错(片段中的MapFragment)

时间:2013-01-29 07:51:35

标签: android-tabhost android-tabs google-maps-android-api-2 android-maps-v2

我有一个fragmentActivity,我在其中构建了一个tabHost,在每个标签中都有一个Fragment。好吧,我有四个标签3的信息和一个片段,其中包括一个谷歌地图-V2(片段中的片段MAp因为我想添加按钮等),当我选择一个标签并传递给其他一切顺利,但是当我选择地图选项卡时,在其他选项卡之后再次选择地图选项卡时会抛出错误。

我的观点似乎是:

|__||__|__|__|
| __________ |
|| FRAGMENT ||
||_____   _ ||
|| FRAG| |_|||
|| MAP |    ||
||     |    ||
||_____|    ||
||__________||

错误:

01-29 08:32:56.979: E/AndroidRuntime(27738): FATAL EXCEPTION: main
01-29 08:32:56.979: E/AndroidRuntime(27738): android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at com.monumentos.vistasmovil.MonumentoMapa.onCreateView(MonumentoMapa.java:127)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1264)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at com.monumentos.vistasmovil.Monumento.onTabChanged(Monumento.java:279)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:436)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.widget.TabHost.setCurrentTab(TabHost.java:421)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:158)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:459)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.view.View.performClick(View.java:2552)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.view.View$PerformClick.run(View.java:9229)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.os.Handler.handleCallback(Handler.java:587)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.os.Looper.loop(Looper.java:138)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.app.ActivityThread.main(ActivityThread.java:3701)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at java.lang.reflect.Method.invokeNative(Native Method)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at java.lang.reflect.Method.invoke(Method.java:507)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at dalvik.system.NativeStart.main(Native Method)
01-29 08:32:56.979: E/AndroidRuntime(27738): Caused by: java.lang.IllegalArgumentException: Binary XML file line #11: Duplicate id 0x7f06005c, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
01-29 08:32:56.979: E/AndroidRuntime(27738):    ... 27 more

错误说:

01-29 08:32:56.979: E/AndroidRuntime(27738): Caused by: java.lang.IllegalArgumentException: Binary XML file line #11: Duplicate id 0x7f06005c, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
01-29 08:32:56.979: E/AndroidRuntime(27738):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
01-29 08:32:56.979: E/AndroidRuntime(27738):    ... 27 more

这是问题,但我不知道为什么只有这个片段有问题:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.monumenos_view_preview, container, false);


return view;
}

1 个答案:

答案 0 :(得分:11)

最后我找到了解决方案,我在片段中删除了嵌套的MapFragment:

public void onDestroyView ()
{
      try{
    SupportMapFragment fragment = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapv2));
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
      }catch(Exception e){
      }
    super.onDestroyView();  
}

我从这里得到解决方案:Fragments within Fragments