借助这些Navigation with Action Bar。我尝试创建一个Action Bar后退按钮。
后退按钮显示在输出中。但是如果我单击后退按钮,则一次发生堆栈跟踪错误。
堆栈跟踪:
E/AndroidRuntime(1846): FATAL EXCEPTION: main
E/AndroidRuntime(1846): Process: com.fth.android, PID: 1846
E/AndroidRuntime(1846): java.lang.IllegalArgumentException: Activity HomeActivity does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data> element in your manifest?)
E/AndroidRuntime(1846): at android.support.v4.app.NavUtils.navigateUpFromSameTask(NavUtils.java:177)
E/AndroidRuntime(1846): at com.sit.fth.activity.HomeActivity.onOptionsItemSelected(HomeActivity.java:160)
E/AndroidRuntime(1846): at android.app.Activity.onMenuItemSelected(Activity.java:2600)
E/AndroidRuntime(1846): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
E/AndroidRuntime(1846): at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:167)
E/AndroidRuntime(1846): at android.view.View.performClick(View.java:4438)
E/AndroidRuntime(1846): at android.view.View$PerformClick.run(View.java:18422)
E/AndroidRuntime(1846): at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime(1846): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(1846): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(1846): at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime(1846): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1846): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(1846): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime(1846): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/AndroidRuntime(1846): at dalvik.system.NativeStart.main(Native Method)
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fth.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:name="com.sit.fth.app.GemsApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@android:style/Theme.Holo.Light" >
<activity
android:name="com.sit.fth.activity.SplashActivity"
android:parentActivityName="com.sit.fth.activity.HomeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.sit.fth.activity.HomeActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.sit.fth.activity.HomeActivity"
android:screenOrientation="portrait" >
</activity>
<activity android:name="com.sit.fth.activity.YoutubePlayActivity" >
</activity>
</application>
</manifest>
HomeActivity.java:
package com.sit.fth.activity;
public class HomeActivity extends BaseActivity implements
VideoFragment.OnVideoSelectedListener,
AboutFragment.OnAboutFragmentSelectedListener,
AnnouncenentFragment.OnAnnouncementSelectedListener,
GalleryFragment.OnGalItemSelectedListener,
MagzineFragment.OnMagzineSelectedListener {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this); // 160 th line Error
return true;
}
return super.onOptionsItemSelected(item);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressLint("NewApi")
public boolean onOptionsItemSelected1(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
TaskStackBuilder.create(this)
.addNextIntentWithParentStack(upIntent)
.startActivities();
} else {
NavUtils.navigateUpTo(this, upIntent);
}
return true;
}
return super.onOptionsItemSelected(item);
}
我不知道如何解决这些问题。任何人都可以帮助我。谢谢。
答案 0 :(得分:1)
java.lang.IllegalArgumentException:Activity HomeActivity没有 指定了父活动名称。 (你有没有忘记添加 你的android.support.PARENT_ACTIVITY元素 清单?)
您需要在清单
中提及家庭活动的父活动http://developer.android.com/training/implementing-navigation/ancestral.html