gpt4 book ai didi

Android:getSupportActionBar() 在 ActionBarSherlock 库中总是返回 null

转载 作者:IT老高 更新时间:2023-10-28 13:16:18 27 4
gpt4 key购买 nike

我正在尝试使用 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。打电话。

相关代码其实在库的demo中:

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) {
}
}

最佳答案

您应该将 Sherlock 主题添加到您的应用程序中

<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="false" android:theme="@style/Theme.Sherlock">

关于Android:getSupportActionBar() 在 ActionBarSherlock 库中总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7294797/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com