Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null pointer Exception at ActionBar

I am getting this Error at runtime.

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

StackTrace:

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

Edited:

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>

Manifest:

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

I didn't know how to solve this.Anyone can help me with this.Thank you.

like image 225
User 10 Avatar asked Jan 22 '15 05:01

User 10


1 Answers

Remove the line

requestWindowFeature(Window.FEATURE_ACTION_BAR);

It is not necessary when using Theme.AppCompat (which adds its own Action Bar). Also ensure your activity extends ActionBarActivity (as is required by Theme.AppCompat) rather than Activity.

When you use ActionBarActivity, you must also use getSupportActionBar() rather than getActionBar().

like image 196
ianhanniballake Avatar answered Sep 22 '22 09:09

ianhanniballake