本文介绍了Android的错误[尝试在空对象引用调用虚拟方法无效android.app.ActionBar']的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有实现viewpager导航抽屉code模块,但是当我运行$ C $词出现以下错误

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):产生的原因:显示java.lang.NullPointerException:尝试调用虚拟方法无效上一个空android.app.ActionBar.setDisplayHomeAsUpEnabled(布尔值)'对象引用九月1日至26日:20:02.959:E / AndroidRuntime(18779):在com.example.tabwithslidingdrawer.MainActivity.onCreate(MainActivity.java:95)

09:20:02.959: E/AndroidRuntime(18779): Caused by:java.lang.NullPointerException: Attempt to invoke virtual method 'voidandroid.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a nullobject reference 01-26 09:20:02.959: E/AndroidRuntime(18779): atcom.example.tabwithslidingdrawer.MainActivity.onCreate(MainActivity.java:95)

指向此行

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

整个code http://pastebin.com/u1K72fr7

entire codehttp://pastebin.com/u1K72fr7

我的清单

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

机器人:主题=@风格/ Theme.Tabwithpager

code http://pastebin.com/EFQLzRej

codehttp://pastebin.com/EFQLzRej

推荐答案

您code被扔在 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);

问题是pretty的简单 - 你是活性被从新继承<$c$c>android.support.v7.app.ActionBarActivity.你应该通过调用<$c$c>getSupportActionBar()代替<$c$c>getActionBar().

如果你看看上面围绕你的code 65行,你会发现你已经这样做:

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

然后放下下跌约87行,它看起来像你想通了相同的:

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

请注意,你怎么注释掉 getActionBar()

这篇关于Android的错误[尝试在空对象引用调用虚拟方法无效android.app.ActionBar']的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:05