我正在尝试使用ActionBarSherlock库为我的 Android 应用程序中的选项卡提供向后兼容的 ActionBar 支持,因此我下载了最新版本,构建了演示并运行了它。
如果你去操作栏,然后选择标签导航,它每次都会崩溃。这是堆栈跟踪:
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): FATAL EXCEPTION: main
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.actionbarsherlock.sample.demos/com.actionbarsherlock.sample.demos.app.ActionBarTabNavigation}: java.lang.NullPointerException
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.os.Looper.loop(Looper.java:132)
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.main(ActivityThread.java:4025)
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at java.lang.reflect.Method.invoke(Method.java:491)
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at dalvik.system.NativeStart.main(Native Method)
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): Caused by: java.lang.NullPointerException
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at com.actionbarsherlock.sample.demos.app.ActionBarTabNavigation.onCreate(ActionBarTabNavigation.java:19)
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): ... 11 more
在解决此问题之前,我无法继续使用我的应用程序。我写了一堆代码,在我的应用程序中设置了操作栏,并尝试运行它,但由于getSupportActionBar()
调用时返回 null 值,它与 NPE 一起崩溃。
相关代码实际上在库的演示中:
public class ActionBarTabNavigation extends FragmentActivity implements ActionBar.TabListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, FragmentStackSupport.CountingFragment.newInstance(0))
.commit();
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 0; i < 3; i++) {
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setText("Tab " + i);
tab.setTabListener(this);
getSupportActionBar().addTab(tab);
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.content, FragmentStackSupport.CountingFragment.newInstance(tab.getPosition()))
.commit();
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}