清单中的错误 parentActivityName

bad parentActivityName in manifest

我正在从 Android 构建您的第一个应用程序。每当我尝试在我的应用程序顶部的导航中按下后退按钮时,我都会收到以下错误:

E/Activity: getParentActivityIntent: bad parentActivityName 
'com.example.amir.myapplication.MainActivity' in manifest
E/NavUtils: getParentActivityIntent: bad parentActivityName 
'com.example.amir.myapplication.MainActivity' in manifest

这是我的 manifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".DisplayMessageActivity"
        android:parentActivityName=".MainActivity">
        <!-- The meta-data tag is required if you support API level 15 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

我的包裹是:包裹com.example.amir.myapplication。这也是我在 buildgradle 中的 applicaitonid。没有 intent-filter 我得到一个错误,需要一个 intent-filter。

DisplayMessageActivity

public class DisplayMessageActivity extends AppCompatActivity {

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

    // Get the Intent that started this activity and extract the string
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Capture the layout's TextView and set the string as its text
    TextView textView = findViewById(R.id.textView2);
    textView.setText(message);
 }
}

MainActivity

public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

/** Called when the user taps the send button */
public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
 }

}

请注意,我对 Java 和 Android 开发完全陌生。我学习了一门 Java 课程,这是我的第一个家庭项目。

您的清单应包括 MainActivity 和 DisplayMessageActivity。现在您只有 DisplayMessageActivity,并且在那里添加了 intent 过滤器。它不应该有 Intent 过滤器,它应该留在 MainActivity 中,因为那是 Intent 所在的地方。

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".DisplayMessageActivity"
        android:parentActivityName=".MainActivity">
        <!-- The meta-data tag is required if you support API level 15 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>
</application>