Null exception in ActionBar

I get this error at runtime.

java.lang.RuntimeException: cannot 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 by:

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 did not know how to solve this. Anyone can help me with this. Thanks.

+6
source share
2 answers

Delete line

 requestWindowFeature(Window.FEATURE_ACTION_BAR); 

When using Theme.AppCompat (which adds its own action bar) is not necessary. Also, make sure your activity extends ActionBarActivity (as required by Theme.AppCompat ), not Activity .

When you use ActionBarActivity , you should also use getSupportActionBar () , not getActionBar() .

+9
source

Line 212

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

Correct: ColorDrawable(Color.parseColor("#FFFFFF"));

0
source

Source: https://habr.com/ru/post/981384/


All Articles