0

i have an android application that i want to enable the up navigation key but the up key for my layout does not display and i get the following NullPointerException from it when i run:

04-20 16:06:48.722 2420-2420/spmrs.mari.com.marispmrs_api19 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: spmrs.mari.com.marispmrs_api19, PID: 2420
                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{spmrs.mari.com.marispmrs_api19/spmrs.mari.com.marispmrs_api19.SmsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                              at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:148)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                           Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                                                                              at spmrs.mari.com.marispmrs_api19.SmsActivity.onCreate(SmsActivity.java:54)
                                                                              at android.app.Activity.performCreate(Activity.java:6237)
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                              at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:148) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Here is my AndroidManifest.xml' SmsActivity

<activity
        android:name=".SmsActivity"
        android:label="@string/title_activity_sms"
        android:theme="@style/AppTheme.NoActionBar"
        android:parentActivityName=".SoilData">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".SoilData" />
    </activity>

And here is the SmsActivity codeblock which give me a null pointer exception:

//        Enabling up navigation
    ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);

Is anyone who can help me with the root cause of the error and how to fix it?

Jens
  • 67,715
  • 15
  • 98
  • 113
Nyerere
  • 29
  • 1
  • 2
  • 8

2 Answers2

1

Your theme is android:theme="@style/AppTheme.NoActionBar" and then you are trying to use ActionBar. ActionBar is not present for that Activity and getSupportActionBar() is not found and will return null. Try a different theme for that Activity which has ActionBar. Example: Theme.AppCompat.Light.DarkActionBar

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
1

Did you set the support actionbar?

Activity Main onCreate:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
if (actionBar != null) {
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowHomeEnabled(true);
}

Activity Main Layout

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary" />
Tom Sabel
  • 3,935
  • 33
  • 45