我有一个带有碎片的导航抽屉,旋转设备并单击菜单后,我的应用程序崩溃了。 我的主要活动代码是:
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;
}
});
}
我的代码在下一段代码中继续像这个logcat错误(第140行):
View.OnClickListener homeOnclickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mDrawerLayout.isDrawerOpen(expListView)){
mDrawerLayout.closeDrawer(expListView);
}else{
mDrawerLayout.openDrawer(expListView);
}
}
};
我的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)
由于
答案 0 :(得分:0)
如果savedInstanceState为null,则只调用setupDrawer。这意味着在重新创建旋转活动时不会调用它。但是,您可以在其中设置mDrawerLayout的值。因此,在旋转后调用onClick时,mDrawerLayout为null。要解决此问题,您需要在两种情况下都设置mDrawerLayout的值。
实际上我不知道为什么当savedInstanceState为非null时你没有调用setupDrawer,这似乎非常错误。
答案 1 :(得分:0)
移动
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
在setUpDrawer()
之前从onCreate()
到if block
。
答案 2 :(得分:0)
好吧,我明白了,不得不修改一些事情,但它有效。
从setUpDrawer()
中取出if null
,就像Gabe Sechan的建议一样。并将其更改为
setUpDrawer();
if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new Inicio()).commit();
}
我还删除了fragment = new Inicio();
和所有getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
并将所有案例更新为case 0:fragment = new enunciados_definiciones(); break;
感谢