6

I'm trying to make a simple android app and am having trouble fixing this bug.

The app has a 4 tab fixed button navigation and the error occurs when on the 4th tab and switching to another tab. Being on any other page works fine but the 4th tab is causing a fatal error every time. There is nothing on that page except a TextView element. Can anyone help me figure out what's wrong or point me in the right direction?

02-17 21:48:54.378: E/AndroidRuntime(5543): FATAL EXCEPTION: main
02-17 21:48:54.378: E/AndroidRuntime(5543): android.view.InflateException: Binary XML file line #12: Error inflating class fragment
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at com.example.testthree.MainActivity$MapSectionFragment.onCreateView(MainActivity.java:241)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1264)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:523)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:495)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:476)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at com.example.testthree.MainActivity.onTabSelected(MainActivity.java:95)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1067)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:489)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.view.View.performClick(View.java:4084)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.view.View$PerformClick.run(View.java:16966)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.os.Handler.handleCallback(Handler.java:615)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.os.Looper.loop(Looper.java:137)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.app.ActivityThread.main(ActivityThread.java:4745)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at java.lang.reflect.Method.invokeNative(Native Method)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at java.lang.reflect.Method.invoke(Method.java:511)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at dalvik.system.NativeStart.main(Native Method)
02-17 21:48:54.378: E/AndroidRuntime(5543): Caused by: java.lang.IllegalArgumentException: Binary XML file line #12: Duplicate id 0x7f04000f, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
02-17 21:48:54.378: E/AndroidRuntime(5543):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
02-17 21:48:54.378: E/AndroidRuntime(5543):     ... 30 more
rogcg
  • 10,451
  • 20
  • 91
  • 133
Derek
  • 4,864
  • 5
  • 28
  • 38

2 Answers2

44

Fixed it with the following code in my map fragment:

public void onDestroyView() {
    super.onDestroyView();
    FragmentManager fm = getActivity().getSupportFragmentManager();
    Fragment fragment = (fm.findFragmentById(R.id.map));
    FragmentTransaction ft = fm.beginTransaction();
    ft.remove(fragment);
    ft.commit();
}
M.Y.
  • 1,105
  • 1
  • 11
  • 30
Derek
  • 4,864
  • 5
  • 28
  • 38
  • 1
    thank you you saved me today :), but what is the problem can you explain please +1 – Syed Raza Mehdi Mar 28 '14 at 09:39
  • 1
    Wow, thanks! saved me a lot of time and headache... – Woody Jun 19 '14 at 13:48
  • 1
    @Derek, thank you for your answer, it helped me fix the crash. Interestingly enough, after I updated appcompat-v7 support library to version 21(with material design) a few days ago, the original bug went away, and there's no need to override onDestroyView() any more. Looks like this crash was caused by a bug in the older version of support library and google has fixed it in v21. – Anton Cherkashyn Nov 14 '14 at 20:30
  • 3
    Better use `ft.commitAllowingStateLoss();` instead because you cannot commit changes after `onSaveInstanceState` – MineConsulting SRL May 06 '15 at 10:05
  • I used this response but at times broke the application. Use this one and it worked perfectly without breaking. If You have problems use this http://stackoverflow.com/a/33335835/3364157 – Emmanuelguther Oct 25 '15 at 22:32
2

While not an expert in tabs, I would appear to be a problem with the map/map fragment.

Are you including the map more than once across all the tabs?

I would try making sure the id of any added fragments (and map fragments) is unique.

You might also find it useful to remove any fragments that you are not using so as to lessen the chance for conflicts. This is likely to be something similar to removing the fragments that inside the tabs that aren't currently displayed. But like I said, I'm not an expert on tabs.

Corey Scott
  • 2,430
  • 3
  • 28
  • 33