I have a navigation drawer with fragments and my app is crashing after I rotate the device and click on the menu. My code on main activity is:
public class MainActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
ImageView home;
Fragment fragment = null;
TextView appname;
ExpandableListView expListView;
HashMap<String, List<String>> listDataChild;
ExpandableListAdapter listAdapter;
List<String> listDataHeader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String fontPath = "neue.ttf";
setContentView(R.layout.activity_main);
home = (ImageView)findViewById(R.id.home);
home.setOnClickListener(homeOnclickListener);
appname = (TextView)findViewById(R.id.appname);
Typeface tf = Typeface.createFromAsset(this.getAssets(), fontPath);
appname.setTypeface(tf);
if(savedInstanceState == null){
setUpDrawer();
}
}
private void setUpDrawer() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));
mDrawerLayout.setDrawerListener(mDrawerListener);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
fragment = new Inicio();
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerLayout.closeDrawer(expListView);
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
switch (groupPosition) {
case 0:
switch (childPosition) {
case 0:
fragment = new enunciados_definiciones();
break;
case 1:
fragment = new enunciados_ayuda_victima();
break;
case 2:
fragment = new enunciados_abordaje();
break;
case 3:
fragment = new transmision_enfermedades();
break;
default:
fragment = new Inicio();
break;
}
break;
case 1:
switch (childPosition) {
case 0:
fragment = new Inicio();
break;
case 1:
fragment = new Inicio();
break;
case 2:
fragment = new Inicio();
break;
default:
break;
}
break;
case 2:
switch (childPosition) {
case 0:
fragment = new Inicio();
break;
case 1:
fragment = new Inicio();
break;
case 2:
fragment = new Inicio();
break;
default:
break;
}
break;
default:
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerLayout.closeDrawer(expListView);
return false;
}
});
}
My code continues like this logcat error (line: 140) its on the next piece of code:
View.OnClickListener homeOnclickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mDrawerLayout.isDrawerOpen(expListView)){
mDrawerLayout.closeDrawer(expListView);
}else{
mDrawerLayout.openDrawer(expListView);
}
}
};
My logcat:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.eiusweb.primerosauxilios.MainActivity$2.onClick(MainActivity.java:140)
at android.view.View.performClick(View.java:4162)
at android.view.View$PerformClick.run(View.java:17082)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
Thanks