通过ACTION_MAIN启动Activity时出现ActivityNotFoundException异常



我正在尝试从另一个具有ACTION_MAIN的活动开始一个活动。我这样做是因为我想让其他应用程序也有可能启动这个次要活动。因此,在我的manifest中,我有两个主要活动:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="main.package"
    android:versionCode="34"
    android:versionName="3.04"
     android:installLocation="preferExternal"
     >
....
    <activity
            android:name=".SplashActivity"
            android:label="@string/app_name" 
             android:icon="@drawable/myicon"
            >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
     <activity
            android:name="main.package.secondary.SecondaryActivity"
            android:label="@string/memorapp"
            android:theme="@style/Theme.Sherlock.Light"
               android:taskAffinity="main.package.secondary.SecondaryActivity"
             >
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

在我的主活动中运行以下代码:

Intent intent = new Intent(Intent.ACTION_MAIN);
   ComponentName cn = new ComponentName("main.package.secondary","main.package.secondary.SecondaryActivity");
   intent.setComponent(cn);                                         
   startActivity(intent);

下面是logcat:

03-28 16:35:41.987: E/AndroidRuntime(9921): FATAL EXCEPTION: main
03-28 16:35:41.987: E/AndroidRuntime(9921): android.content.ActivityNotFoundException: Unable to find explicit activity class {main.package.secondary/main.package.secondary.secondaryMainActivity}; have you declared this activity in your AndroidManifest.xml?
03-28 16:35:41.987: E/AndroidRuntime(9921):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at android.app.Activity.startActivityForResult(Activity.java:2827)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:674)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at android.app.Activity.startActivity(Activity.java:2933)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at main.package.MainActivity.onMenufrag(MainActivity.java:206)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at main.package.MenuFragment.sendBodyTextToActivity(MenuFragment.java:92)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at main.package.MenuFragment.access$0(MenuFragment.java:90)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at main.package.MenuFragment$1.onClick(MenuFragment.java:66)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at android.view.View.performClick(View.java:2501)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at android.view.View$PerformClick.run(View.java:9107)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at android.os.Handler.handleCallback(Handler.java:587)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at android.os.Looper.loop(Looper.java:130)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at android.app.ActivityThread.main(ActivityThread.java:3835)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at java.lang.reflect.Method.invoke(Method.java:507)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
03-28 16:35:41.987: E/AndroidRuntime(9921):     at dalvik.system.NativeStart.main(Native Method)

你能告诉我为什么我的活动没有找到,或者我应该怎么做,以便我的SecondaryActivity启动

你能试试吗?

Intent intent = new Intent(Intent.ACTION_MAIN);
ComponentName cn = new ComponentName("main.package","main.package.secondary.SecondaryActivity");
intent.setComponent(cn);
startActivity(intent);

第一个参数接受"应用程序包名"

尝试为第二个活动设置以下意图过滤器:

<intent-filter>
    <action android:name="main.package.secondary.SecondaryActivity" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

输入manifest.xml:

<activity android:name="main.package.secondary.SecondaryActivity" android:label="@string/memorapp" android:theme="@style/Theme.Sherlock.Light" android:taskAffinity="main.package.secondary.SecondaryActivity">  </activity>

相关内容

  • 没有找到相关文章

最新更新