java - 从 Activity 开始的由 fragment 启动

  显示原文与译文双语对照的内容

我有一个使用标签的应用程序。 我使用 Fragments 来实现 TabListeners 。 其中一个 fragment 启动 2 activity ( 根据某些按钮) 。

我使用 Eclipse 接口创建这些 activity,这意味着 Eclipse 负责所有的工作( 创建布局,更新 Manifest 等等 ) 。

如果我使用电话上的返回按钮从 Activity 返回,则没有问题。 但是如果在 Activity 布局的左上角使用"<"符号,我有一个错误:


01-04 02:56:56.000 E/Activity( 7556): getParentActivityIntent: bad parentActivityName 'com.android.nfcinfo2.FragmentCeSupport' in manifest

01-04 02:56:56.007 E/NavUtils( 7556): getParentActivityIntent: bad parentActivityName 'com.android.nfcinfo2.FragmentCeSupport' in manifest

01-04 02:56:56.007 D/AndroidRuntime( 7556): Shutting down VM**

01-04 02:56:56.007 W/dalvikvm( 7556): threadid=1: thread exiting with uncaught exception (group=0x41c78b90)

01-04 02:56:56.007 E/AndroidRuntime( 7556): FATAL EXCEPTION: main

01-04 02:56:56.007 E/AndroidRuntime( 7556): Process: com.android.nfcinfo2, PID: 7556

01-04 02:56:56.007 E/AndroidRuntime( 7556): java.lang.IllegalArgumentException: Activity EvtTransactionActivity does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data> element in your manifest?)

01-04 02:56:56.007 E/AndroidRuntime( 7556): at android.support.v4.app.NavUtils.navigateUpFromSameTask(NavUtils.java:177)

但是看起来我的Manifest 是好的( FragmentCeSupport是父级,EvtTransaction和SeRouting是子 activity ):


 <application
 android:allowBackup="true"
 android:icon="@drawable/stnfcinfo"
 android:label="@string/app_name"
 android:theme="@style/AppTheme"> 
 <activity
 android:name="com.android.nfcinfo2.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.android.nfcinfo2.EvtTransactionActivity"
 android:label="@string/title_activity_evt_transaction"
 android:parentActivityName="com.android.nfcinfo2.FragmentCeSupport"> 
 <meta-data
 android:name="android.support.PARENT_ACTIVITY"
 android:value="com.android.nfcinfo2.FragmentCeSupport"/>
 </activity>
 <activity
 android:name="com.android.nfcinfo2.SeRouting"
 android:label="@string/title_activity_se_routing"
 android:parentActivityName="com.android.nfcinfo2.FragmentCeSupport"> 
 <meta-data
 android:name="android.support.PARENT_ACTIVITY"
 android:value="com.android.nfcinfo2.FragmentCeSupport"/>
 </activity>
 </application>

下面是我从FragmentCeSupport启动 activity的方法:


@Override
public void onClick(View v)
{
 switch (v.getId())
 {
 case R.id.evt_tx_button:
 {
 Intent intent = new Intent(getActivity(), EvtTransactionActivity.class);
 startActivity(intent);
 }
 break;

 case R.id.routing_host_button:
 {
 Intent intent = new Intent(getActivity(), SeRouting.class);
 startActivity(intent);
 }
 break;
 }
}

下面是EvtTransactionActivity中的返回代码( 由 Eclipse 生成):


@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);
}

时间: 作者:


Activity EvtTransactionActivity does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data> element in your manifest?)

更改


 <activity
 android:name="com.android.nfcinfo2.EvtTransactionActivity"
 android:label="@string/title_activity_evt_transaction"
 android:parentActivityName="com.android.nfcinfo2.FragmentCeSupport">//is this the activity you want to go to. i guess this is a fragment
 <meta-data
 android:name="android.support.PARENT_ACTIVITY"
 android:value="com.android.nfcinfo2.FragmentCeSupport"/>
 </activity>


 <activity
 android:name="com.android.nfcinfo2.EvtTransactionActivity"
 android:label="@string/title_activity_display_message"
 android:parentActivityName="com.android.nfcinfo2.MainActivity"> 
 <!-- Parent activity meta-data to support 4.0 and lower -->
 <meta-data
 android:name="android.support.PARENT_ACTIVITY"
 android:value="com.android.nfcinfo2.MainActivity"/>
 </activity>

作者:

如果希望从子 Activity 到 fragment,请在 Manifest 文件中放置以下代码


 android:launchMode="singleTop" 

有关更多参考,请参见这里的 快乐编码:)

...