Reputation: 1359
I've been going through the android basic training and have just done the action bar training and am looking to run the program. However, when I click the "back" button on the actionbar in the emulator, the app crashes with the logcat saying:
02-02 21:46:29.279: E/AndroidRuntime(1361): java.lang.IllegalArgumentException: Activity DisplayMessageActivity does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data> element in your manifest?)
02-02 21:46:29.279: E/AndroidRuntime(1361): at android.support.v4.app.NavUtils.navigateUpFromSameTask(NavUtils.java:177)
02-02 21:46:29.279: E/AndroidRuntime(1361): at com.example.myfirstapptwo.DisplayMessageActivity.onOptionsItemSelected(DisplayMessageActivity.java:62)
02-02 21:46:29.279: E/AndroidRuntime(1361): at android.app.Activity.onMenuItemSelected(Activity.java:2502)
02-02 21:46:29.279: E/AndroidRuntime(1361): at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:161)
02-02 21:46:29.279: E/AndroidRuntime(1361): at android.view.View.performClick(View.java:3511)
02-02 21:46:29.279: E/AndroidRuntime(1361): at android.view.View$PerformClick.run(View.java:14105)
02-02 21:46:29.279: E/AndroidRuntime(1361): at android.os.Handler.handleCallback(Handler.java:605)
02-02 21:46:29.279: E/AndroidRuntime(1361): at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 21:46:29.279: E/AndroidRuntime(1361): at android.os.Looper.loop(Looper.java:137)
02-02 21:46:29.279: E/AndroidRuntime(1361): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-02 21:46:29.279: E/AndroidRuntime(1361): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 21:46:29.279: E/AndroidRuntime(1361): at java.lang.reflect.Method.invoke(Method.java:511)
02-02 21:46:29.279: E/AndroidRuntime(1361): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-02 21:46:29.279: E/AndroidRuntime(1361): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-02 21:46:29.279: E/AndroidRuntime(1361): at dalvik.system.NativeStart.main(Native Method)
However, the android.support.PARENT_ACTIVITY metadata is most definitely in my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapptwo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myfirstapptwo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfirstapptwo.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>
The java code that I'm running is:
package com.example.myfirstapptwo;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class DisplayMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {@link android.app.ActionBar}.
*/
@SuppressLint("NewApi")
private void setupActionBar() {
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
I'm pretty stumped, given that I followed the tutorial instructions to the letter and the error message is telling me something that I'm pretty certain isn't true, so was wondering if anyone had any ideas on what could be causing this?
Upvotes: 0
Views: 1907
Reputation: 14710
You have a typo in your manifest: instead of
<activity
android:name="com.example.myfirstapptwo.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
you should have:
<activity
android:name="com.example.myfirstapptwo.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapptwo.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapptwo.MainActivity" />
</activity>
Upvotes: 4