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

4 回答 4

8

你 Actionbar 返回 null 因为你没有 actionbar 但你正在使用 getActionbar()

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

于 2013-03-04T16:46:44.860 回答
6

你删除

android:theme="@style/AppTheme"

在 AndroidManifest.xml 的“应用程序”中

于 2013-05-16T07:27:35.880 回答
1

如果您删除该行,您真的会看到一个操作栏actionBar.show()吗?如果不是,它可能与您的主题有关,例如Theme.Light.NoActionBar.

于 2013-03-04T16:40:30.807 回答
0

转到 AndroidManifest.xml 并替换

android:theme="@style/AppTheme"

经过

android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
于 2016-01-29T04:54:31.150 回答