ActionBar上的空指针异常

时间:2015-01-22 05:17:21

标签: java android nullpointerexception stack-trace android-logcat

我在运行时收到此错误。

  

java.lang.RuntimeException:无法启动活动ComponentInfo   android.util.AndroidRuntimeException:必须调用requestFeature()   在添加内容之前

堆栈跟踪:

01-22 04:55:59.728: E/AndroidRuntime(2443): FATAL EXCEPTION: main
01-22 04:55:59.728: E/AndroidRuntime(2443): Process: com.qrme.quranmadeeasy, PID: 2443
01-22 04:55:59.728: E/AndroidRuntime(2443): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.qrme.quranmadeeasy/com.qrme.quranmadeeasy.ChapterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setBackgroundDrawable(android.graphics.drawable.Drawable)' on a null object reference
01-22 04:55:59.728: E/AndroidRuntime(2443):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
01-22 04:55:59.728: E/AndroidRuntime(2443):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
01-22 04:55:59.728: E/AndroidRuntime(2443):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
01-22 04:55:59.728: E/AndroidRuntime(2443):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
01-22 04:55:59.728: E/AndroidRuntime(2443):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-22 04:55:59.728: E/AndroidRuntime(2443):     at android.os.Looper.loop(Looper.java:135)
01-22 04:55:59.728: E/AndroidRuntime(2443):     at android.app.ActivityThread.main(ActivityThread.java:5221)
01-22 04:55:59.728: E/AndroidRuntime(2443):     at java.lang.reflect.Method.invoke(Native Method)
01-22 04:55:59.728: E/AndroidRuntime(2443):     at java.lang.reflect.Method.invoke(Method.java:372)
01-22 04:55:59.728: E/AndroidRuntime(2443):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-22 04:55:59.728: E/AndroidRuntime(2443):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-22 04:55:59.728: E/AndroidRuntime(2443): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setBackgroundDrawable(android.graphics.drawable.Drawable)' on a null object reference
01-22 04:55:59.728: E/AndroidRuntime(2443):     at com.qrme.quranmadeeasy.ChapterActivity.ActionBar(ChapterActivity.java:212)
01-22 04:55:59.728: E/AndroidRuntime(2443):     at com.qrme.quranmadeeasy.ChapterActivity.initialize(ChapterActivity.java:112)
01-22 04:55:59.728: E/AndroidRuntime(2443):     at com.qrme.quranmadeeasy.ChapterActivity.onCreate(ChapterActivity.java:68)
01-22 04:55:59.728: E/AndroidRuntime(2443):     at android.app.Activity.performCreate(Activity.java:5933)
01-22 04:55:59.728: E/AndroidRuntime(2443):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
01-22 04:55:59.728: E/AndroidRuntime(2443):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
01-22 04:55:59.728: E/AndroidRuntime(2443):     ... 10 more

编辑:

ChapterActivity.java:

import android.support.v7.app.ActionBarActivity;

public class ChapterActivity extends ActionBarActivity implements OnItemClickListener {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    //  requestWindowFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_chapter);

        initialize();  --->68th line
}
private void initialize() {

    listChapter.setDivider(new ColorDrawable(Color
                .parseColor(separator_grey)));
        listChapter.setDividerHeight(2);
    ............

    ActionBar();  -->112th line
    }

public void ActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(Color   --->212th line
                .parseColor(white)));  
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        LayoutInflater mInflater = LayoutInflater.from(this);

         .........
        actionBar.setCustomView(mCustomView);
        actionBar.setDisplayShowCustomEnabled(true);
    } 
}

styles.xml:

<resources>

    <style name="AppBaseTheme" parent="@style/Theme.AppCompat"></style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
</resources>

清单:

  <application
        android:name="com.qrme.quranmadeeasy.Application"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"            
               >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

我不知道如何解决这个问题。任何人都可以帮助我。谢谢。

2 个答案:

答案 0 :(得分:9)

删除行

requestWindowFeature(Window.FEATURE_ACTION_BAR);

使用Theme.AppCompat(添加自己的操作栏)时没有必要。同时确保您的活动延伸ActionBarActivityTheme.AppCompat所要求的)而不是Activity

使用ActionBarActivity时,您还必须使用getSupportActionBar()而不是getActionBar()

答案 1 :(得分:0)

第212行

错误: ColorDrawable(Color.parseColor(white));

正确: ColorDrawable(Color.parseColor("#FFFFFF"));