【发布时间】:2016-03-09 04:53:16
【问题描述】:
在下面的 SSCCE 中,我尝试从 preferences.xml 调用 显式 意图,以打开位于应用程序中唯一一个包中的 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>
注意:我没有在清单中为SecondActivity 使用<intent-filter>,因为它与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);
}
}
【问题讨论】:
-
@Solace 您的代码看起来不错,请记住您尝试实现的相同用例我尝试了相同的代码并且它工作正常。您可以发布其他文件以查找此问题的根本原因。
-
@HammadTariqSahi (1) 但是为什么我需要
action来表示意图?这是一个明确的意图;我想启动一个特定的活动(并且我提供了它的名称),而不是任何可以处理视图意图的活动。 (2)我使用完全限定名称,如您在我提供的preferences.xml 文件中所见(3)没有子包,只有一个包包含所有活动,如问题中所述。 -
你的代码应该是这样的。它工作得很好。尝试清理构建项目并重新安装 apk。或通过 Skype 与我联系,以便我通过屏幕共享查看。
-
@Solace 唯一的区别是您在清单中的声明不是完全限定的,但我认为这不重要,也尝试在清单中使用完全限定,然后告诉我。
标签: android android-intent android-preferences activitynotfoundexception android-preference-v14