Android ActionBar 空指针异常

6

我想启用Android ActionBar,但是它对我来说不起作用。

这是我的MainActivity

public void onCreate(Bundle savedInstanceState) {

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);


        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        ActionBar actionBar = getActionBar();
        actionBar.show();
        //more code............

以下是logcat日志:

03-04 16:31:09.423: E/AndroidRuntime(1441): FATAL EXCEPTION: main
03-04 16:31:09.423: E/AndroidRuntime(1441): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.salebook/com.example.salebook.MainActivity}: java.lang.NullPointerException
03-04 16:31:09.423: E/AndroidRuntime(1441):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-04 16:31:09.423: E/AndroidRuntime(1441):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-04 16:31:09.423: E/AndroidRuntime(1441):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-04 16:31:09.423: E/AndroidRuntime(1441):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-04 16:31:09.423: E/AndroidRuntime(1441):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 16:31:09.423: E/AndroidRuntime(1441):     at android.os.Looper.loop(Looper.java:137)
03-04 16:31:09.423: E/AndroidRuntime(1441):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-04 16:31:09.423: E/AndroidRuntime(1441):     at java.lang.reflect.Method.invokeNative(Native Method)
03-04 16:31:09.423: E/AndroidRuntime(1441):     at java.lang.reflect.Method.invoke(Method.java:511)
03-04 16:31:09.423: E/AndroidRuntime(1441):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-04 16:31:09.423: E/AndroidRuntime(1441):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-04 16:31:09.423: E/AndroidRuntime(1441):     at dalvik.system.NativeStart.main(Native Method)
03-04 16:31:09.423: E/AndroidRuntime(1441): Caused by: java.lang.NullPointerException
03-04 16:31:09.423: E/AndroidRuntime(1441):     at com.example.salebook.MainActivity.onCreate(MainActivity.java:130)
03-04 16:31:09.423: E/AndroidRuntime(1441):     at android.app.Activity.performCreate(Activity.java:5104)
03-04 16:31:09.423: E/AndroidRuntime(1441):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-04 16:31:09.423: E/AndroidRuntime(1441):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-04 16:31:09.423: E/AndroidRuntime(1441):     ... 11 more

当第130行是anctionBar.show()时,在res/menu/menu.xml中我有:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/itemid_0"
        android:title="Action Item 0"
        android:icon="@drawable/ic_launcher"
        android:orderInCategory="0"
        android:showAsAction="ifRoom|withText" />
    <item android:id="@+id/itemid_1"
        android:title="Action Item 1"
        android:orderInCategory="0" />
    <item android:id="@+id/itemid_2"
        android:title="Action Item 2"
        android:orderInCategory="0" />
    <item android:id="@+id/itemid_3"
        android:title="Action Item 3"
        android:orderInCategory="0" />
</menu>

我没能找到问题所在,希望有人能帮我。

谢谢。

此外,在appThem样式中:

<style name="AppTheme" parent="AppBaseTheme">
        <item name="android:windowActionBar">true</item>
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

1
请看这里:https://dev59.com/e2XWa4cB1Zd3GeqPQ9pr - Georgy Gobozov
我刚刚调试了它,发现 actionBar 是 null... 我看了这篇帖子,但没有帮助到我。 - Adir Rahamim
我也添加了这一行,就像链接中写的那样: getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 然而仍然出现相同的错误。 - Adir Rahamim
3
显然你没有一个action_bar。 - njzk2
@AdirRahamim 你试过我建议的吗? - Pragnani
显示剩余4条评论
4个回答

8

6
您删除
android:theme="@style/AppTheme"

从 AndroidManifest.xml 中的 "application" 开始


1
如果您删除actionBar.show()这行代码,是否会看到操作栏?如果没有,可能与您的主题有关,例如Theme.Light.NoActionBar

仍然出现NullPointerException,尽管我已经删除了这行代码。 - Adir Rahamim
你的应用/活动有自定义主题吗? - nhaarman
在 manifest.xml 文件中,我有这一行:android:theme="@style/AppTheme"。 - Adir Rahamim
AppTheme 样式中声明了什么? - nhaarman
<style name="AppTheme" parent="AppBaseTheme"> <item name="android:windowActionBar">true</item> <!-- 所有不特定于特定API级别的自定义内容都可以放在这里。 --> </style>,我也将其添加到了主线程中。 - Adir Rahamim

0

前往AndroidManifest.xml并替换

android:theme="@style/AppTheme"

android:theme="@android:style/Theme.Holo.Light.DarkActionBar"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接