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);
I also tried changing topics. For example, android:theme="@android:style/Theme.Holo.Light"
.
source share