6

This question is asked several times in stackoverflow and I have tried all of them. But unfortunately neither is working for me.

I am trying to implement the navigation between two activities, as part of learning Android app development. My minium SDK and target SDK versions are 11 and 21 (Android 5), respectively. My settings in AndroidManifest.xml are shown below:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.navigation"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
             >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name=".DetailActivity" >       
        </activity>
    </application>
</manifest>

I have two activities: MainActivity and DetailActivity. When I click a button in MainActivity, the app successfully opens the DetailActivity. But when I am trying to enable the back button by using the following code, it returns a NullPointerExcepion:

getActionBar().setDisplayHomeAsUpEnabled(true);

My both the classes extend ActionBarActivity.

In MainActivity.java:

public class MainActivity extends ActionBarActivity {
...
}

In DetailActivity.java:

public class DetailActivity extends ActionBarActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        getActionBar().setDisplayHomeAsUpEnabled(true); // returns null pointer
}

I have also tried changing the themes. For example, android:theme="@android:style/Theme.Holo.Light".

novice20
  • 356
  • 1
  • 3
  • 10
  • possible duplicate of [getActionBar() returns null](http://stackoverflow.com/questions/6867076/getactionbar-returns-null) – SMA Nov 22 '14 at 14:31

4 Answers4

24

You are inheriting from ActionBarActivity. Hence, you need to use getSupportActionBar(), not getActionBar(), to get at the appcompat-v7-supplied action bar backport.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
4

import v7:

 import android.support.v7.app.ActionBar;

then in the onCreate method:

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
ShadowGod
  • 7,891
  • 3
  • 28
  • 31
AI I
  • 331
  • 5
  • 15
3

Use this..

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

instead of this - getActionBar().setDisplayHomeAsUpEnabled(true);

it will work perfectly.

Mahendra
  • 31
  • 1
  • 2
0

Summay: To make sure you won't get a NullPointerException. You need to:

  1. Import the right library target to you minium SDK version.
  2. Use the right themes, just as the question owner said.

But in my situation an if statement is necessary to solve my App from crash. By the way, I'm using an AppCompatActivity to hold my view fragment.

public onCreateView(LayoutInflater inflater, final ViewGroup container,Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.list_fragment, container, false);
    ActionBar actionBar = getActivity().getActionBar();
    if (actionBar != null){
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
VenMia0
  • 1
  • 1