我正在尝试在我的应用中实现PreferenceActivity
,但我不断收到android.content.ActivityNotFoundException
类型的例外情况。它提到确保Activity列在我的Manifest文件中,但它确实存在并且我仍然得到例外。在此先感谢您的帮助 - 这一直让我发疯。
这是一个类似的帖子我发现有一个解决方案,但它对我不起作用: 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中启动Preferences的代码:
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
答案 0 :(得分:7)
类名在异常中看起来很有趣:
com.areyling.myapp/java.util.prefs.Preferences
我认为您可能会以某种方式引用不正确的Preference类。你有导入语句导入java.util.prefs.Preferences吗?如果您尝试使用完全限定名称启动活动会发生什么?:
Intent preferencesIntent = new Intent(this, com.areyling.myapp.Preferences.class);
答案 1 :(得分:3)
我知道这篇文章已经有两年多了,但每当我试图搜索远离这个问题的任何东西时,这篇文章都会突然出现!
如果对我来说就是这种情况,那么我想其他许多人就是这种情况。我按照上面的描述进行了操作,并将完整列表放在我的MainActivity.java
中Intent preferencesIntent = new Intent(this, com.areyling.myapp.Preferences.class);
但是这个技巧在我的MAINIFEST文件中也提供了相同的完整列表
<activity android:name="com.areyling.myapp.Preferences.class"></activity>
这就是我最终的伎俩!
同样,我知道这篇文章远远超出它的年龄,但我认为试图找到不同的搜索引擎短语的帖子一直带我到这个页面,我也可以为其他任何人更新它同样的问题。谢谢!
答案 2 :(得分:0)
我遇到了同样的问题。我将Intent i2 = new Intent(this, NoteEditActivity.class);
声明为类变量,这似乎给了我这个错误。 (之所以我作为一个类变量这样做是因为我在i2
方法中使用了onItemClick
,这不允许我在那里声明它。我把它移到了一个方法
public void begin(int position, long id)
{
Intent i2 = new Intent(this, NoteEditActivity.class);
.....
}
并修复了错误。