Hi I am making an application where I have to display a map, the map will show you to select a menu option from the drawer. At first I had a problem, but I put the following code and it worked:
public void onDestroyView() {
super.onDestroyView();
MapFragment fragment = ((MapFragment)getFragmentManager().findFragmentById(R.id.map));
if (fragment != null) {
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
}
The problem came when I use the back button on the device and when I select the same option from the menu drawer. He throws me the following exception and the application stops:
11-01 05:53:07.359: E/AndroidRuntime(1189): FATAL EXCEPTION: main
11-01 05:53:07.359: E/AndroidRuntime(1189): java.lang.NullPointerException
11-01 05:53:07.359: E/AndroidRuntime(1189): at
android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1410)
11-01 05:53:07.359: E/AndroidRuntime(1189): at
android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
11-01 05:53:07.359: E/AndroidRuntime(1189): at
android.os.Handler.handleCallback(Handler.java:800)
11-01 05:53:07.359: E/AndroidRuntime(1189): at
android.os.Handler.dispatchMessage(Handler.java:100)
11-01 05:53:07.359: E/AndroidRuntime(1189): at android.os.Looper.loop(Looper.java:194)
11-01 05:53:07.359: E/AndroidRuntime(1189): at
android.app.ActivityThread.main(ActivityThread.java:5371)
11-01 05:53:07.359: E/AndroidRuntime(1189): at java.lang.reflect.Method.invokeNative(Native
Method)
11-01 05:53:07.359: E/AndroidRuntime(1189): at
java.lang.reflect.Method.invoke(Method.java:525)
11-01 05:53:07.359: E/AndroidRuntime(1189): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
11-01 05:53:07.359: E/AndroidRuntime(1189): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-01 05:53:07.359: E/AndroidRuntime(1189): at dalvik.system.NativeStart.main(Native Method)
To change the options I'm using the following code in the activity
private void mostrarFragment(int position){
Fragment fragment = null;
System.out.println("opcion: " + position);
editorPrefs = prefs.edit();
editorPrefs.putInt("position", position);
editorPrefs.commit();
switch (position) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
case 3:
fragment = new Fragment4();
break;
case 4:
fragment = new Fragment5();
break;
}
if(fragment != null){
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, fragment);
transaction.commit();
drawerList.setItemChecked(position, true);
//drawerList.setSelection(position);
drawerList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getActionBar().setTitle(listaOpciones.get(position).getNombre());
drawerMenu.closeDrawer(drawerList);
}
}
Thank you for your help