开发者

ActivityNotFound exception thrown for a PreferenceActivity listed in the Manifest xml

I'm trying to implement a PreferenceActivity in my app, but I keep getting an exception of type android.content.ActivityNotFoundException. It mentions making sure that the Activity is listed in my Manifest file, but it is and I still get the exception. Thanks in advance for your help--this is has been driving me crazy.

This is a similar posting I found that has a solution, but it didn't work for me: 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)
        }
    }

The manifest file:

<?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>

The code attempting to launch the Preferences in Main.java:

开发者_如何学编程Intent preferencesIntent = new Intent(this, Preferences.class);
startActivity(preferencesIntent);

The logcat output:

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


The class name looks funny in the exception:

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

I think you may be referring to the incorrect Preference class somehow. Do you have an import statement that's importing java.util.prefs.Preferences? What happens if you try to launch the activity using it's fully qualified name?:

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


I know this post is over two years old but every time I attempted to search for anything remotely close to this problem, this post CONTINUALLY popped up!

If that is the case for me, then I would imagine it would be the case for many others. I followed the suggestion as described above and put the full listing in my MainActivity.java

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

But what did the trick was put the same full listing in my MAINIFEST file as well

<activity android:name="com.areyling.myapp.Preferences.class"></activity>

This is what finally did me the trick!

Again, I know this post is well beyond its age, but I figured that attempting to find different posts with slightly different search engine phrases kept on bringing me to this page, I may as well update it for anyone else that is having this same problem. Thanks!


I had trouble with the same problem. I was declaring Intent i2 = new Intent(this, NoteEditActivity.class); as a class variable, which seemed to give me that error. (The reason I was doing it as a class variable is because I was using i2 in an onItemClick method which would not let me declare it there. I moved it to a method

public void begin(int position, long id)
{
    Intent i2 = new Intent(this, NoteEditActivity.class);  
    .....   
}

and that fixed the error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜