ActionBar 出现空指针异常

Null pointer Exception at ActionBar

我在运行时遇到这个错误。

java.lang.RuntimeException: Unable to start activity ComponentInfo android.util.AndroidRuntimeException: requestFeature() must be called before adding content

堆栈跟踪:

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.access0(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>

我不知道怎么解决this.Anyone可以帮我this.Thank你

删除行

requestWindowFeature(Window.FEATURE_ACTION_BAR);

使用 Theme.AppCompat 时没有必要(它添加了自己的操作栏)。还要确保您的 activity 扩展 ActionBarActivity(根据 Theme.AppCompat 的要求)而不是 Activity.

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

第 212 行

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

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