ActivityNotFoundException 试图从首选项调用 'explicit' 意图以在默认包中打开 Activity 时?

ActivityNotFoundException when trying to invoke an 'explicit' intent from Preferences, to open an Activity in default package?

在下面的 SSCCE 中,我试图从 preferences.xml 调用一个 explicit 意图,以打开一个位于唯一的 Activity应用程序中的包,其中包含所有 Activity。

但我得到以下异常:

android.content.ActivityNotFoundException: No Activity found to handle Intent {  }

我见过 this question 但它是关于在另一个包中启动一个 Activity,有人在那个问题中说他们的应用程序打开 Activity默认包工作正常。

以下是代码的相关部分。

注意: 由于 SecondActivityMainActivity 在同一个包中,我最初尝试只使用 一个 android:targetClass 属性到 preferences.xml 中的 <intent>,但在异常之后我也添加了 android:targetPackage,但这并没有解决问题。

MainActivty.java:

package practice.preferences_practice;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class MainActivity extends PreferenceActivity {

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <Preference android:key="@+id/preferences_preference_preferenceContainingIntent"
        android:title="@string/preferences_preference_preferenceContainingIntent_title"
        android:summary="@string/preferences_preference_preferenceContainingIntent_summary" >


        <intent android:targetPackage="practice.preferences_practice"
            android:targetClass="practice.preferences_practice.SecondActivity" />


    </Preference>

</PreferenceScreen>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="practice.preferences_practice"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="23" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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=".SecondActivity"
            android:label="@string/title_activity_second" >
        </activity>
    </application>

</manifest>

注意: 我没有在 SecondActivity 的清单中使用 <intent-filter> 因为它与 [=21= 在同一个默认包中],也就是 practice.preferences_practice

注意: 如果您认为我也应该 post 所有其他代码文件,请告诉我。




编辑:

res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Preferences Practice</string>
    <string name="hello_world">Hello world!</string>

    <string name="preferences_preference_preferenceContainingIntent_title">Preferece Title</string>
    <string name="preferences_preference_preferenceContainingIntent_summary">Opens another activity because this preference contains and invokes an Intent.</string>

    <string name="title_activity_second">SecondActivity</string>

</resources>

res/layout/activity_second.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F4A460"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

SecondActivity.java:

package practice.preferences_practice;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class SecondActivity extends Activity {

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

使用startActivity(new Intent(MainActivity.this, SecondActivity.class));开始第二个activity。

你的代码是应该的。它工作得很好,因为我也测试过它。尝试清理构建项目并重新安装 apk。