【问题标题】:ActivityNotFoundException when trying to invoke an 'explicit' intent from Preferences, to open an Activity in default package?尝试从首选项调用“显式”意图以在默认包中打开活动时出现 ActivityNotFoundException?
【发布时间】:2016-03-09 04:53:16
【问题描述】:

在下面的 SSCCE 中,我尝试从 preferences.xml 调用 显式 意图,以打开位于应用程序中唯一一个包中的 Activity,所有 Activity 都位于该包中.

但我得到以下异常:

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

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

以下是代码的相关部分。

注意:由于SecondActivityMainActivity在同一个包中,我最初尝试只使用一个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 使用&lt;intent-filter&gt;,因为它与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


【解决方案1】:

您的代码应如此。它工作得很好,因为我也对其进行了测试。尝试清理构建项目并重新安装 apk。

【讨论】:

  • 也为我工作。也就是说,这个答案感觉不完整。
【解决方案2】:

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

【讨论】:

  • 这不能回答问题。我知道如何以编程方式从另一个 Activity 启动一个 Activity。问题是关于为什么首选项中指定的 Intent 不能成功启动 Activity?我错过了什么?我在哪里犯了错误?我建议你在它被否决之前删除这个答案。
猜你喜欢
  • 1970-01-01
  • 2014-10-10
  • 1970-01-01
  • 1970-01-01
  • 2015-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多