使用setDisplayHomeAsUpEnabled返回清单中的PARENT_ACTIVITY的问题

时间:2018-02-10 22:53:37

标签: java android android-manifest

我有两个java文件,NewContactViewContact

在这两个代码中,我的代码都是这样的,它在NewContact中起作用:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_contact);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ActionBar actionbar = getSupportActionBar();
    actionbar.setDisplayHomeAsUpEnabled(true);

    //show the App title
    actionbar.setTitle("MyApp");

    }

//code for the '<', back button. Go back to PopulistoListView, as defined
//in Manifest, PARENT_ACTIVITY
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

我的manifest(从<application></application>)是这样的:

<application
        android:name="com.example.chris.tutorialspoint.AppController"
        android:allowBackup="true"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity
            android:name="com.example.chris.tutorialspoint.VerifyUserPhoneNumber"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.chris.tutorialspoint.DisplayMyPopulistoListView"
            android:launchMode="singleInstance" />
        <activity
            android:name="com.example.chris.tutorialspoint.PopulistoListView"
            android:launchMode="singleInstance" />
        <activity android:name="com.example.chris.tutorialspoint.CountryCodes" />
        <activity
            android:name="com.example.chris.tutorialspoint.ViewContact"
            android:noHistory="true" />
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.chris.tutorialspoint.PopulistoListView" />

        <activity android:name="com.example.chris.tutorialspoint.EditContact" />
        <activity
            android:name="com.example.chris.tutorialspoint.NewContact"
            android:configChanges="orientation|screenSize">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.chris.tutorialspoint.PopulistoListView" />
        </activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
     App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

然而,当我点击'<'中的ViewContact按钮时,我收到错误:

 java.lang.IllegalArgumentException: Activity ViewContact does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data>  element in your manifest?)
                                                                                      at android.support.v4.app.NavUtils.navigateUpFromSameTask(NavUtils.java:178)
                                                                                      at com.example.chris.tutorialspoint.ViewContact.onOptionsItemSelected(ViewContact.java:405)
                                                                                      at android.app.Activity.onMenuItemSelected(Activity.java:2681)
                                                                                      at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:403)
                                                                                      at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:189)

然而,您可以看到我的parent activity中定义了Manifest。我做错了什么?

1 个答案:

答案 0 :(得分:4)

如果你仔细观察你的清单,特别是这部分:

<activity
        android:name="com.example.chris.tutorialspoint.ViewContact"
        android:noHistory="true" />
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.chris.tutorialspoint.PopulistoListView" />

您会看到在<activity>元素之前关闭<meta-data>代码,这意味着<meta-data>与整个<application>相关联object(有效,但不是你想要的)。

更新您的清单,以便<meta-data>元素位于<activity>元素中:

<activity
        android:name="com.example.chris.tutorialspoint.ViewContact"
        android:noHistory="true">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.chris.tutorialspoint.PopulistoListView" />
 </activity>