即使在清单中声明后,ActivityNotFoundException也是如此

时间:2012-10-04 06:07:41

标签: android android-intent

当我想要向偏好设置屏幕启动意图时,我遇到了一个强制关闭。 即使在清单中宣布它之后,我仍然遇到了这个问题。

清单

<activity android:name=".Preferences" ></activity>

从MainActivity启动意图

Intent startPreference = new Intent(MainActivity.this,Preferences.class);
startActivity(startPreference);

偏好设置屏幕

public class Preferences extends PreferenceActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);   
    }
}

然而每当我打开意图时,我都会遇到

10-04 14:02:45.088: E/AndroidRuntime(11201): android.content.ActivityNotFoundException: Unable to find explicit activity class {dev.example.activity/java.util.prefs.Preferences}; have you declared this activity in your AndroidManifest.xml?

编辑: 完整清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="dev.example.activity"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="Example"
        android:theme="@android:style/Theme.Light.NoTitleBar" >
        <activity
            android:name=".MainActivity"
            android:label="Example" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Individual"
            android:label="Example" >
        </activity>
        <activity android:name="dev.example.activity.Preferences" >
        </activity>
    </application>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

</manifest>

完整logcat

10-04 14:24:35.408: E/AndroidRuntime(12936): FATAL EXCEPTION: main
10-04 14:24:35.408: E/AndroidRuntime(12936): android.content.ActivityNotFoundException: Unable to find explicit activity class {dev.example.activity/java.util.prefs.Preferences}; have you declared this activity in your AndroidManifest.xml?
10-04 14:24:35.408: E/AndroidRuntime(12936):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at android.app.Activity.startActivityForResult(Activity.java:2827)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at android.app.Activity.startActivity(Activity.java:2933)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at dev.shaunidiot.mycolorscreen.MainActivity.onOptionsItemSelected(MainActivity.java:166)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at android.app.Activity.onMenuItemSelected(Activity.java:2205)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:768)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:147)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at android.view.View$PerformClick.run(View.java:9229)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at android.os.Handler.handleCallback(Handler.java:587)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at android.os.Looper.loop(Looper.java:130)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at android.app.ActivityThread.main(ActivityThread.java:3701)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at java.lang.reflect.Method.invokeNative(Native Method)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at java.lang.reflect.Method.invoke(Method.java:507)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
10-04 14:24:35.408: E/AndroidRuntime(12936):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

您指的是java.util.prefs.Preferences:那不是您的活动。 以这种方式创建意图:

Intent startPreference = new Intent(MainActivity.this,             
                           dev.example.activity.Preferences.class);
startActivity(startPreference);

答案 1 :(得分:0)

请使用

声明您的活动全名
  

com.packagename.classname

我认为它可以解决您的错误。

编辑: -

你的活动扩展了 PreferenceActivity ,我忘了看,以便宣布你的活动如下。

<activity android:name=".Preferences" android:label="@string/prefTitle"
    android:screenOrientation="nosensor">
    <intent-filer>
        <action android:name=".Preferences" />
        <category android:name="android.intent.cetagory.PREFERENCE" />
    </intent-filer>

答案 2 :(得分:0)

只需将此行添加到清单

即可
 <activity
    android:name=".Preferences"
    android:label="@string/set_preferences">
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT"/>

    </intent-filter>  
</activity>

如果您收到错误,仍然可以。click this