我正在尝试在我的应用程序中实现PreferenceActivity,但是我一直收到类型为android.content.ActivityNotFoundException的异常。它提到要确保“活动”列在我的清单文件中,但确实如此,但我仍然遇到异常。预先感谢您的帮助-这一直使我发疯。

这是我发现的类似帖子,具有解决方案,但对我不起作用:
My PreferenceActivity does not show up, even though it is in my manifest file

res / xml / preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        android:title="@string/preferences_title_settings"
        android:key="@string/preferences_key_settings" >
    <PreferenceScreen android:summary="@string/preferences_summary_gameplaySettings"
            android:title="@string/preferences_title_gameplaySettings"
            android:key="@string/preferences_key_gameplaySettings">
        <ListPreference android:entries="@array/entries_difficulty"
                android:entryValues="@array/entryvalues_difficulty"
                android:dialogTitle="@string/dialog_title_difficulty"
                android:title="@string/preferences_title_difficulty"
                android:key="@string/preferences_key_difficulty"
                android:summary="@string/preferences_summary_difficulty"
                android:defaultValue="0" />
        <CheckBoxPreference android:key="@string/preferences_key_autosave"
                android:title="@string/preferences_title_autosave"
                android:summary="@string/preferences_summary_autosave" />
    </PreferenceScreen>
</PreferenceScreen>


Preferences.java:

public class Preferences extends PreferenceActivity implements
    OnSharedPreferenceChangeListener {
        private SharedPreferences preferences;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
            addPreferencesFromResource(R.xml.preferences);
            getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this)
        }
    }


清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.areyling.myapp"
      android:versionCode="1"
      android:versionName="@string/app_version" >
    <uses-sdk android:minSdkVersion="4" />
    <application android:icon="@drawable/icon"
            android:theme="@android:style/Theme.NoTitleBar"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
        <activity android:name=".Main"
                  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=".Tutorial"
                android:screenOrientation="portrait"
                android:configChanges="keyboardHidden|orientation" />
        <activity android:name=".About" />
        <activity android:name=".Preferences"
                android:screenOrientation="portrait"
                android:theme="@android:style/Theme" />
    </application>
</manifest>


试图在Main.java中启动首选项的代码:

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


logcat输出:

03-19 16:57:39.365: DEBUG/AndroidRuntime(448): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
03-19 16:57:39.365: DEBUG/AndroidRuntime(448): CheckJNI is ON
03-19 16:57:39.605: DEBUG/AndroidRuntime(448): --- registering native functions ---
03-19 16:57:39.935: DEBUG/ddm-heap(448): Got feature list request
03-19 16:57:41.025: INFO/ActivityManager(52): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.areyling.myapp/.Main }
03-19 16:57:41.308: DEBUG/AndroidRuntime(448): Shutting down VM
03-19 16:57:41.308: DEBUG/dalvikvm(448): DestroyJavaVM waiting for non-daemon threads to exit
03-19 16:57:41.314: DEBUG/dalvikvm(448): DestroyJavaVM shutting VM down
03-19 16:57:41.314: DEBUG/dalvikvm(448): HeapWorker thread shutting down
03-19 16:57:41.325: DEBUG/dalvikvm(448): HeapWorker thread has shut down
03-19 16:57:41.325: DEBUG/jdwp(448): JDWP shutting down net...
03-19 16:57:41.325: INFO/dalvikvm(448): Debugger has detached; object registry had 1 entries
03-19 16:57:41.334: DEBUG/dalvikvm(448): VM cleaning up
03-19 16:57:41.386: ERROR/AndroidRuntime(448): ERROR: thread attach failed
03-19 16:57:41.395: DEBUG/dalvikvm(448): LinearAlloc 0x0 used 639500 of 5242880 (12%)
03-19 16:57:41.594: INFO/ActivityManager(52): Start proc com.areyling.myapp for activity com.areyling.myapp/.Main: pid=455 uid=10028 gids={}
03-19 16:57:41.811: DEBUG/ddm-heap(455): Got feature list request
03-19 16:57:42.854: DEBUG/(455): unable to unlink '/data/data/com.areyling.myapp/shared_prefs/com.areyling.myapp_preferences.xml.bak': No such file or directory (errno=2)
03-19 16:57:43.605: INFO/ActivityManager(52): Displayed activity com.areyling.myapp/.Main: 2081 ms (total 2081 ms)
03-19 16:57:48.834: DEBUG/dalvikvm(207): GC freed 80 objects / 3920 bytes in 105ms
03-19 16:57:53.905: DEBUG/dalvikvm(100): GC freed 2224 objects / 130960 bytes in 184ms
03-19 16:57:57.385: INFO/ActivityManager(52): Starting activity: Intent { cmp=com.areyling.myapp/java.util.prefs.Preferences }
03-19 16:57:57.395: DEBUG/AndroidRuntime(455): Shutting down VM
03-19 16:57:57.405: WARN/dalvikvm(455): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
03-19 16:57:57.405: ERROR/AndroidRuntime(455): Uncaught handler: thread main exiting due to uncaught exception
03-19 16:57:57.434: ERROR/AndroidRuntime(455): java.lang.IllegalStateException: Could not execute method of the activity
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.view.View$1.onClick(View.java:2031)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.view.View.performClick(View.java:2364)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.view.View.onTouchEvent(View.java:4179)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.view.View.dispatchTouchEvent(View.java:3709)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.os.Looper.loop(Looper.java:123)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.app.ActivityThread.main(ActivityThread.java:4363)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at java.lang.reflect.Method.invokeNative(Native Method)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at java.lang.reflect.Method.invoke(Method.java:521)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at dalvik.system.NativeStart.main(Native Method)
03-19 16:57:57.434: ERROR/AndroidRuntime(455): Caused by: java.lang.reflect.InvocationTargetException
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at com.areyling.myapp.Main.settingsButtonClick(Main.java:105)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at java.lang.reflect.Method.invokeNative(Native Method)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at java.lang.reflect.Method.invoke(Method.java:521)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.view.View$1.onClick(View.java:2026)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     ... 21 more
03-19 16:57:57.434: ERROR/AndroidRuntime(455): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.areyling.myapp/java.util.prefs.Preferences}; have you declared this activity in your AndroidManifest.xml?
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.app.Activity.startActivityForResult(Activity.java:2749)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     at android.app.Activity.startActivity(Activity.java:2855)
03-19 16:57:57.434: ERROR/AndroidRuntime(455):     ... 25 more
03-19 16:57:57.484: INFO/Process(52): Sending signal. PID: 455 SIG: 3
03-19 16:57:57.494: INFO/dalvikvm(455): threadid=7: reacting to signal 3

最佳答案

类名看起来很有趣,但例外:

 com.areyling.myapp/java.util.prefs.Preferences


我认为您可能以某种方式引用了错误的Preference类。您是否有要导入java.util.prefs.Preferences的import语句?如果您尝试使用活动的全限定名启动活动,该怎么办?:

 Intent preferencesIntent = new Intent(this, com.areyling.myapp.Preferences.class);

10-08 14:50