我一直在进行android基础训练,刚刚完成了动作栏训练,我正在寻找运行程序。但是,当我单击模拟器中操作栏上的“后退”按钮时,应用程序会与logcat崩溃说:
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)
然而,android.support.PARENT_ACTIVITY元数据绝对是我的清单:
<?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>
我正在运行的java代码是:
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);
}
}
我很难过,因为我按照教程说明写了这封信,错误信息告诉我一些我很确定不是真的,所以想知道是否有人有什么想法可能是什么引起这个?
答案 0 :(得分:3)
您的清单中有拼写错误:而不是
<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>
你应该:
<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>