android.content.ActivityNotFoundException:无法在 Android 中找到显式活动类

Posted

技术标签:

【中文标题】android.content.ActivityNotFoundException:无法在 Android 中找到显式活动类【英文标题】:android.content.ActivityNotFoundException: Unable to find explicit activity class in Android 【发布时间】:2015-11-17 07:01:17 【问题描述】:

在 MainActivity 类中,我有以下代码可以转到 settings.class 。

Intent intent1 = new Intent(MainActivity.this,Settings.class);
startActivity(intent1);

但我有例外。 logcat 显示以下错误。

11-17 12:31:50.245: E/androidRuntime(11166): android.content.ActivityNotFoundException: Unable to find explicit activity class com.commlink.messaging/com.commlink.messaging.Settings; have you declared this activity in your AndroidManifest.xml?
11-17 12:31:50.245: E/AndroidRuntime(11166):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1634)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1430)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at android.app.Activity.startActivityForResult(Activity.java:3507)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at android.app.Activity.startActivityForResult(Activity.java:3459)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at android.app.Activity.startActivity(Activity.java:3710)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at android.app.Activity.startActivity(Activity.java:3678)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at com.commlink.messaging.MainActivity.onOptionsItemSelected(MainActivity.java:105)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at android.app.Activity.onMenuItemSelected(Activity.java:2674)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1074)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:206)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at android.widget.AbsListView.performItemClick(AbsListView.java:1152)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3014)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at android.widget.AbsListView$3.run(AbsListView.java:3865)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at android.os.Handler.handleCallback(Handler.java:808)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at android.os.Handler.dispatchMessage(Handler.java:103)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at android.os.Looper.loop(Looper.java:193)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at android.app.ActivityThread.main(ActivityThread.java:5330)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at java.lang.reflect.Method.invokeNative(Native Method)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at java.lang.reflect.Method.invoke(Method.java:515)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
11-17 12:31:50.245: E/AndroidRuntime(11166):    at dalvik.system.NativeStart.main(Native Method)

我已经在 menifest.xml 中贴上了 Settings.class 。我的menifest.xml 文件如下:

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

        <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/MyAppTheme"  >
        <activity
            android:name="com.commlink.messaging.MainActivity"
            android:label="@string/app_name"
            android:uiOptions="splitActionBarWhenNarrow" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>    
             <meta-data android:name="android.support.UI_OPTIONS"
                   android:value="splitActionBarWhenNarrow" /> 
             <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
             </intent-filter>

            <meta-data
                 android:name="android.app.searchable"
                 android:resource="@xml/searchable" />        
        </activity>

        <activity
            android:name="com.commlink.messaging.New_Message"
            android:label="@string/app_name"  >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter> 
        </activity>

        <activity
            android:name="com.commlink.messaging.Settings"
            android:label="@string/app_name"  >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter> 
        </activity>


          <receiver android:name=".SmsBroadcastReceiver"
                android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_DELIVER" /> 
            </intent-filter>
        </receiver>

        <!-- BroadcastReceiver that listens for incoming MMS messages -->
        <receiver android:name=".MmsReceiver"
            android:permission="android.permission.BROADCAST_WAP_PUSH">
            <intent-filter>
                <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
                <data android:mimeType="application/vnd.wap.mms-message" />
            </intent-filter>
        </receiver>

        <!-- Activity that allows the user to send new SMS/MMS messages -->
        <activity android:name=".ComposeSmsActivity"
            android:theme="@style/AppTheme" >
            <intent-filter>
                <action android:name="android.intent.action.SEND" />                
                <action android:name="android.intent.action.SENDTO" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </activity>

        <!-- Service that delivers messages from the phone "quick response" -->
        <service android:name=".HeadlessSmsSendService"
                 android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
                 android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </service>
    </application>
</manifest>

我能做些什么来解决这个异常?

【问题讨论】:

Settings Activity中删除&lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.DEFAULT" /&gt; &lt;/intent-filter&gt; Clean and built 你的项目再试一次.. 您真的应该切换到 Studio。 Eclipse 有这些烦人的问题。您可以尝试删除“构建”文件夹,重新启动 Eclipse 并进行清理和构建。通常这会解决问题。 使用 Android Studio 获得更好的方法 argillander.wordpress.com/2011/11/25/… 【参考方案1】:

像这样更改您的AndroidManifest.xml。 像这样更改您的 android:name 。因为您的包名称已经在 AndroidManifest.xml 的顶部构建。

   <activity
        android:name=".Settings"
        android:label="@string/app_name"  >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter> 
    </activity>

【讨论】:

我已经做到了。但我也有同样的例外。 你的回答有什么不同吗? 显示您的应用层次结构。因为根据您的错误,您已添加到相同的包名称 我认为问题不在代码中。这是 Eclipse 的一个传奇问题。很高兴 Google 放弃了 AS。

以上是关于android.content.ActivityNotFoundException:无法在 Android 中找到显式活动类的主要内容,如果未能解决你的问题,请参考以下文章