在下面的 SSCCE 中,我尝试从 preferences.xml
调用显式 Intent 。 ,打开一个 Activity,该 Activity 位于应用程序中唯一的包中,所有 Activity 都位于该包中。
但我得到以下异常:
android.content.ActivityNotFoundException: No Activity found to handle Intent { }
我见过this question但这是关于在另一个包中启动一个 Activity ,有人在这个问题中说他们的应用程序在默认包中打开 Activity 工作正常。
以下是相关代码部分。
注意:自SecondActivity
起与 MainActivity
位于同一包中,我最初尝试仅使用一个android:targetClass
属性为<intent>
在preferences.xml中,但在异常之后我添加了 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>
注意:我没有使用过 <intent-filter>
在 list 中为SecondActivity
因为它与 MainActivity
位于相同的默认包中,即practice.preferences_practice
。
注意:如果您认为我也应该发布所有其他代码文件,请告诉我。
编辑:
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);
}
}
最佳答案
你的代码应该是这样的。它工作得很好,因为我也测试了它。尝试清理构建项目并重新安装 apk。
关于android - 尝试从首选项调用 'explicit' Intent 以打开默认包中的 Activity 时出现 ActivityNotFoundException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35883157/