我正在为我的应用程序设置一个标签主机,我注意到我声明每个标签的顺序很重要。设置完成后,我的应用程序在到达活动之前意外退出:
TabSpec spec1 = th.newTabSpec("tag1");
TabSpec spec2 = th.newTabSpec("tag2");
TabSpec spec3 = th.newTabSpec("tag3");
spec1.setContent(R.id.tab1);
spec2.setContent(R.id.tab2);
spec3.setContent(R.id.tab3);
spec1.setIndicator("Stop Watch");
spec2.setIndicator("Tab 2");
spec3.setIndicator("Tab 3");
th.addTab(spec1);
th.addTab(spec2);
th.addTab(spec3);
如此设置,应用程序运行正常:
TabSpec spec1 = th.newTabSpec("tag1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("StopWatch");
th.addTab(spec1);
TabSpec spec2 = th.newTabSpec("tag2");
spec2.setContent(R.id.tab2);
spec2.setIndicator("Tab 2");
th.addTab(spec2);
TabSpec spec3 = th.newTabSpec("tag3");
spec3.setContent(R.id.tab3);
spec3.setIndicator("Add Tab");
th.addTab(spec3);
为什么一个人工作与否?下面显示了(部分)控制台和logCat输出: 控制台:
ActivityManager: Warning: Activity not started, its current task has been brought to the front
logcat的:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.secondapp/com.example.secondapp.MangaList}: java.lang.NullPointerException
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x41b8a2a0)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1882)
at com.example.secondapp.MangaList.<init>(MangaList.java:15)