Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ActionBar NullPointerException

I want to enable the Android ActionBar, however it doesn't 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 the 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

Where line 130 is anctionBar.show() and I have in 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 didn't find the problem. Hope someone help me.

Thanks.

Also,in the 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>
like image 898
Adir Rahamim Avatar asked Mar 04 '13 16:03

Adir Rahamim


2 Answers

You Actionbar returns null because you don't have actionbar but you are requesting using getActionbar()

Make sure that you've window feature to show your actionbar, That is required for the actionbar to show. Check the below links for more details. ...check this question getActionBar returns null And also check this link for the more information on your problem http://blog.perpetumdesign.com/2011/08/strange-case-of-dr-action-and-mr-bar.html

like image 52
Pragnani Avatar answered Oct 18 '22 02:10

Pragnani


You Remove

android:theme="@style/AppTheme"

in "application" from AndroidManifest.xml

like image 45
Satheesh Avatar answered Oct 18 '22 00:10

Satheesh