1

我得到 ActivityNotFoundException 没有任何明显的原因。我是 Android 新手,所以我需要帮助。

我从 onOptionsItemSelected 创建了菜单并运行活动:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
   public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.settings:
         startActivity(new Intent(this, Settings.class));
         return true;
      }
      return false;
   }

设置类:

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

菜单.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/settings"
    android:title="@string/action_settings"/>
</menu>

设置.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory>
<ListPreference
   android:title="@string/menu_language_settings"
   android:key="languagePreferences"
   android:entries="@array/langPreferencesArray"
   android:entryValues="@array/langPreferencesValues" />
<Preference
   android:title="@string/menu_help"
   android:key="helpPreferences" /> 
</PreferenceCategory>
</PreferenceScreen>  

当然,我在清单中添加了活动声明:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.symbolrecogniser"
android:versionCode="1"
android:versionName="1.0" >

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.symbolrecogniser.StartAppActivity"
        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="com.example.symbolrecogniser.Settings"
        android:label="@string/settings_title">
    </activity>
</application>

>

有人有什么想法吗?

这是logCat:

05-13 01:07:44.169: E/AndroidRuntime(23122): FATAL EXCEPTION: main
05-13 01:07:44.169: E/AndroidRuntime(23122): android.content.ActivityNotFoundException:  Unable to find explicit activity class {com.example.symbolrecogniser/android.provider.Settings}; have you declared this activity in your AndroidManifest.xml?
05-13 01:07:44.169: E/AndroidRuntime(23122):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at android.app.Activity.startActivityForResult(Activity.java:2827)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at android.app.Activity.startActivity(Activity.java:2933)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at com.example.symbolrecogniser.StartAppActivity.onOptionsItemSelected(StartAppActivity.java:34)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at android.app.Activity.onMenuItemSelected(Activity.java:2205)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:756)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at android.view.View$PerformClick.run(View.java:9080)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at android.os.Handler.handleCallback(Handler.java:587)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at android.os.Looper.loop(Looper.java:123)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at android.app.ActivityThread.main(ActivityThread.java:3729)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at java.lang.reflect.Method.invokeNative(Native Method)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at java.lang.reflect.Method.invoke(Method.java:507)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
05-13 01:07:44.169: E/AndroidRuntime(23122):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

2

您确定 <manifest> 标记的“包”属性中声明的包与 Settings 类所在的包(不是子包)相同吗?

如果没有,您必须在清单的“android:name”属性中声明具有全名的活动类,包括包。

于 2013-05-12T21:40:36.007 回答
1

检查第一个代码片段所在文件的“import”语句。某处可能有

import android.provider.Settings;

它导入了错误的设置类。

于 2013-05-12T22:31:44.400 回答