Android ActionBar NullPointerException

I want to enable Android ActionBar, but this does not work for me.

Here is my MainActivity :

public void onCreate(Bundle savedInstanceState) { this.requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.login); ActionBar actionBar = getActionBar(); actionBar.show(); //more code............ 

and here is logcat:

 03-04 16:31:09.423: E/AndroidRuntime(1441): FATAL EXCEPTION: main 03-04 16:31:09.423: E/AndroidRuntime(1441): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.salebook/com.example.salebook.MainActivity}: java.lang.NullPointerException 03-04 16:31:09.423: E/AndroidRuntime(1441): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 03-04 16:31:09.423: E/AndroidRuntime(1441): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 03-04 16:31:09.423: E/AndroidRuntime(1441): at android.app.ActivityThread.access$600(ActivityThread.java:141) 03-04 16:31:09.423: E/AndroidRuntime(1441): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 03-04 16:31:09.423: E/AndroidRuntime(1441): at android.os.Handler.dispatchMessage(Handler.java:99) 03-04 16:31:09.423: E/AndroidRuntime(1441): at android.os.Looper.loop(Looper.java:137) 03-04 16:31:09.423: E/AndroidRuntime(1441): at android.app.ActivityThread.main(ActivityThread.java:5041) 03-04 16:31:09.423: E/AndroidRuntime(1441): at java.lang.reflect.Method.invokeNative(Native Method) 03-04 16:31:09.423: E/AndroidRuntime(1441): at java.lang.reflect.Method.invoke(Method.java:511) 03-04 16:31:09.423: E/AndroidRuntime(1441): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 03-04 16:31:09.423: E/AndroidRuntime(1441): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 03-04 16:31:09.423: E/AndroidRuntime(1441): at dalvik.system.NativeStart.main(Native Method) 03-04 16:31:09.423: E/AndroidRuntime(1441): Caused by: java.lang.NullPointerException 03-04 16:31:09.423: E/AndroidRuntime(1441): at com.example.salebook.MainActivity.onCreate(MainActivity.java:130) 03-04 16:31:09.423: E/AndroidRuntime(1441): at android.app.Activity.performCreate(Activity.java:5104) 03-04 16:31:09.423: E/AndroidRuntime(1441): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 03-04 16:31:09.423: E/AndroidRuntime(1441): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 03-04 16:31:09.423: E/AndroidRuntime(1441): ... 11 more 

If line 130 is anctionBar.show() , and I have res / menu / menu.xml :

 <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/itemid_0" android:title="Action Item 0" android:icon="@drawable/ic_launcher" android:orderInCategory="0" android:showAsAction="ifRoom|withText" /> <item android:id="@+id/itemid_1" android:title="Action Item 1" android:orderInCategory="0" /> <item android:id="@+id/itemid_2" android:title="Action Item 2" android:orderInCategory="0" /> <item android:id="@+id/itemid_3" android:title="Action Item 3" android:orderInCategory="0" /> </menu> 

I did not find a problem. Hope someone helps me.

Thanks.

Also in appThem style:

 <style name="AppTheme" parent="AppBaseTheme"> <item name="android:windowActionBar">true</item> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> 
+6
source share
4 answers

. Actionbar returns null because you do not have an action bar, but you are requesting the use of getActionbar ()

Make sure you have a window function to show your action bar. This is necessary to display the action bar. See the links below for more information. ... check this question getActionBar returns null And also check this link for more information about your problem http://blog.perpetumdesign.com/2011/08/strange-case-of-dr-action-and-mr-bar .html

+7
source

You delete

 android:theme="@style/AppTheme" 

in the "application" of AndroidManifest.xml

+6
source

Do you really see the action bar if you delete the line actionBar.show() ? If not, it could be related to your topic, for example. Theme.Light.NoActionBar .

+1
source

go to AndroidManifest.xml and replace

 android:theme="@style/AppTheme" 

by

 android:theme="@android:style/Theme.Holo.Light.DarkActionBar" 
0
source

All Articles