当前位置: 首页 > 知识库问答 >
问题:

Android错误[尝试对空对象引用调用虚拟方法“void Android.app.ActionBar”]

麹繁
2023-03-14

我有一个用导航抽屉实现viewpager的代码模块,然而,当我运行代码时,我得到以下错误

01-26 09:20:02.958: D/AndroidRuntime(18779): Shutting down VM
01-26 09:20:02.959: E/AndroidRuntime(18779): FATAL EXCEPTION: main
01-26 09:20:02.959: E/AndroidRuntime(18779): Process: com.example.tabwithslidingdrawer, PID: 18779
01-26 09:20:02.959: E/AndroidRuntime(18779): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tabwithslidingdrawer/com.example.tabwithslidingdrawer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
01-26 09:20:02.959: E/AndroidRuntime(18779):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
01-26 09:20:02.959: E/AndroidRuntime(18779):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
01-26 09:20:02.959: E/AndroidRuntime(18779):    at android.app.ActivityThread.html" target="_blank">access$800(ActivityThread.java:148)
01-26 09:20:02.959: E/AndroidRuntime(18779):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
01-26 09:20:02.959: E/AndroidRuntime(18779):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-26 09:20:02.959: E/AndroidRuntime(18779):    at android.os.Looper.loop(Looper.java:135)
01-26 09:20:02.959: E/AndroidRuntime(18779):    at android.app.ActivityThread.main(ActivityThread.java:5312)
01-26 09:20:02.959: E/AndroidRuntime(18779):    at java.lang.reflect.Method.invoke(Native Method)
01-26 09:20:02.959: E/AndroidRuntime(18779):    at java.lang.reflect.Method.invoke(Method.java:372)
01-26 09:20:02.959: E/AndroidRuntime(18779):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
01-26 09:20:02.959: E/AndroidRuntime(18779):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
01-26 09:20:02.959: E/AndroidRuntime(18779): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
01-26 09:20:02.959: E/AndroidRuntime(18779):    at com.example.tabwithslidingdrawer.MainActivity.onCreate(MainActivity.java:95)
01-26 09:20:02.959: E/AndroidRuntime(18779):    at android.app.Activity.performCreate(Activity.java:5953)
01-26 09:20:02.959: E/AndroidRuntime(18779):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
01-26 09:20:02.959: E/AndroidRuntime(18779):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
01-26 09:20:02.959: E/AndroidRuntime(18779):    ... 10 more
09:20:02.959: E/AndroidRuntime(18779): Caused by:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null
object reference 01-26 09:20:02.959: E/AndroidRuntime(18779):     at
com.example.tabwithslidingdrawer.MainActivity.onCreate(MainActivity.java:95)

指向此行

// enabling action bar app icon and behaving it as a toggle button
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

我的舱单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tabwithslidingdrawer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Tabwithpager" >
        <activity
            android:name="com.example.tabwithslidingdrawer.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Android:theme=“@style/theme.TabWithPager”

每当发生这样的错误时

1.检查正在使用哪种活动,是简单的Android.app活动还是AppCompatActivity或ActionBarActivity等等。

2.检查扩展的活动类型是否属于compat类别

如果它属于appCompat,我们使用getSupportActionBar()。对于Android.app类型,我们可以使用getActionBar()

示例:在清单文件中,如果应用的主题是android:theme=“@android:style/theme.holo.dialog”getActionBar()将起作用

共有1个答案

陆文博
2023-03-14

您的代码在com.example.TabWithSlidingDrawer.mainactivity.onCreate(mainactivity.java:95):

        // enabling action bar app icon and behaving it as toggle button
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

问题很简单-您的活动是从新的android.support.v7.app.ActionBaractivity继承的。您应该使用对GetSupportActionBar()的调用,而不是对GetActionBar()的调用。

如果您查看上面代码的第65行,就会发现您已经在这样做了:

        actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        // TODO: Remove the redundant calls to getSupportActionBar()
        //       and use variable actionBar instead
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

然后在87号线附近向下看,你得出了同样的结论:

        getSupportActionBar().setTitle(
                        Html.fromHtml("<font color=\"black\">" + mTitle + " - "
                                        + menutitles[0] + "</font>"));
        // getActionBar().setTitle(mTitle +menutitles[0]);

注意您是如何注释掉getActionBar()的。

 类似资料: