GetActionBar (). setDisplayHomeAsUpEnabled (true) throws a NullPointerException

This question is asked several times in stackoverflow, and I tried all of them. But, unfortunately, I do not work for me.

I am trying to implement navigation between two actions as part of learning Android applications. My mini SDKs and target SDKs 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 the button in MainActivity, the application successfully opens DetailActivity. But when I try to turn on the back button using the following code, it returns NullPointerExcepion:

 getActionBar().setDisplayHomeAsUpEnabled(true); 

My both 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 also tried changing topics. For example, android:theme="@android:style/Theme.Holo.Light" .

+6
source share
4 answers

Inherited from ActionBarActivity . Therefore, you need to use getSupportActionBar() , not getActionBar() , to go to the appcompat-v7 -supplied action bar backpord.

+24
source

import v7:

  import android.support.v7.app.ActionBar; 

then in the onCreate method:

 ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); 
+3
source

Use this.

getSupportActionBar () setDisplayHomeAsUpEnabled (true) ;.

getActionBar () instead. setDisplayHomeAsUpEnabled (true);

It will work perfectly.

+2
source

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

  • Import the target library into the mini SDK.
  • Use the right topics, as the question owner said.

But in my situation, the if statement needs to be solved for my application due to a crash. By the way, I use AppCompatActivity to store my presentation 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); } 
0
source

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


All Articles