Android ActionBar NullPointerException [英] Android ActionBar NullPointerException

查看:91
本文介绍了Android ActionBar NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启用Android ActionBar,但是它对我不起作用.

I want to enable the Android ActionBar, however it doesn't work for me.

这是我的 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 中:

Where line 130 is anctionBar.show() and I have in 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>

我没有发现问题.希望有人帮助我.

I didn't find the problem. Hope someone help me.

谢谢.

此外,在appThem样式中:

Also,in the appThem style:

<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>

推荐答案

您的操作栏返回null,因为您没有操作栏,但您正在使用getActionbar()请求

You Actionbar returns null because you don't have actionbar but you are requesting using getActionbar()

确保您具有显示操作栏的窗口功能,这是显示操作栏所必需的.检查以下链接以获取更多详细信息. ...检查此问题 getActionBar返回null 并检查此链接以获取有关您的问题的更多信息 http://blog.perpetumdesign .com/2011/08/strange-case-of-dr-action-and-mr-bar.html

Make sure that you've window feature to show your actionbar, That is required for the actionbar to show. Check the below links for more details. ...check this question getActionBar returns null And also check this link for the more information on your problem http://blog.perpetumdesign.com/2011/08/strange-case-of-dr-action-and-mr-bar.html

这篇关于Android ActionBar NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆