3

I am developing an App with TabHost, which contains 4 different Tabviews. Each tabview contains different fragment views added with following code

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.tabcontent, fragment);
ft.commit();

The 3rd Tab contains a Google Maps View extends SupportMapFragment.

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.map_layout, container,
                            false);
        //Calls Asynctask to fetch map location details from server
        return view;       
      }

From AsyncTask onPostExecute, The following code is used to plot markers on Google Maps :

mFragment = new SupportMapFragment() {
@Override
public void OnActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);    
if ((map = mFragment.getMap()) != null) {                                   
    setUpMap();
      }
 }

};  
 FragmentManager mangaer = this.getActivity()
                                    .getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = mangaer
                                    .beginTransaction();
    fragmentTransaction.add(R.id.new_map_layout, mFragment);
    fragmentTransaction.commit();

Now When I add map fragments for the first time, everything works normal and mapview is getting plotted on the Tab view. But while testing the App, changing the tabviews frequently without any delay, the App crashes from 3rd Tab with the following error in transaction.commit :

E/AndroidRuntime(4392): java.lang.IllegalArgumentException: No view found for id 0x7f060218 for fragment CustomMap$4{42e3cea0 #3> id=0x7f060218}

E/AndroidRuntime(4392): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:865)

Please suggest what changes I need to make to avoid the crash.

Note : The crash only happens when I change the Tabs frequently

Timson
  • 1,337
  • 3
  • 19
  • 32
  • Try moving the fragment creation to the onResume method of your map tab instead of in the onCreate. It might not be able to initialize the activity fast enough which results in an error. Reference: http://developer.android.com/reference/android/app/Activity.html – Remy Baratte Mar 26 '14 at 07:31

0 Answers0