Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

Android Error [Attempt to invoke virtual method 'void android.app.ActionBar' on a null object reference]

I have a code module which implements viewpager with navigation drawer, however, when I run the code I get the following error

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

points to this line

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

entire code http://pastebin.com/u1K72fr7

My manifest

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

code http://pastebin.com/EFQLzRej

================================================================== EDIT/UPDATE:

What I learnt from this

Whenever such an error occurs

1. Check what kind of Activity is being used, is it a simple android.app Activity or an AppCompatActivity or an ActionBarActivity and so on.

2. Check if your activity type which is extended falls under the compat category

example android.app based Activity/Fragment are non appCompat types, whereas android.support.v4.app.Fragment or android.support.v4.app.ActivityCompat are appCompat based

if it falls under appCompat we use getSupportActionBar() else for android.app types we can use getActionBar()

3. Check the theme applied to the activity in question in the manifest file

example: In the manifest file if theme applied is say android:theme="@android:style/Theme.Holo.Dialog" getActionBar() will work

but if theme applied for the activity in the manifest is as follows android:theme="@style/Theme.AppCompat.Light" then you have to use getSupportActionBar()

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code is throwing on 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);

The problem is pretty simple- your Activity is inheriting from the new android.support.v7.app.ActionBarActivity. You should be using a call to getSupportActionBar() instead of getActionBar().

If you look above around line 65 of your code you'll see that you're already doing that:

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

And then lower down around line 87 it looks like you figured out the same:

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

Notice how you commented out getActionBar().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...