java - 使用 setDisplayHomeAsUpEnabled 返回 list 中的 PARENT_ACTIVITY 时出现问题

标签 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 中定义。我做错了什么?

最佳答案

如果您仔细查看 list ,特别是这部分:

<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>对象(这是有效的,但不是你想要的)。

更新您的 list ,以便 <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>

关于java - 使用 setDisplayHomeAsUpEnabled 返回 list 中的 PARENT_ACTIVITY 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48726434/

相关文章:

android - 对 Android 的 'preferExternal' list 过滤器感到困惑

android - 启用 Google App Signing 后出现 "The AndroidManifest.xml of the uploaded APK could not be parsed. Was it compiled properly?"错误

java - 如何显示整个 double 值?

android - WebView 不会为某些网页滚动

java - Android 中字符串到整数的高效映射

android - 不受信任的 SSL 证书 - 自定义 keystore 问题

android - AndroidManifest.xml 文件的问题

java - iText 嵌套表 - 第一行未呈现

java - 找不到 org.aspectj.weaver.reflect.ReflectionWorld

Java + JSON 和 "errorjava.lang.RuntimeException: Stub!"错误