1

我正在尝试使用 ACTION_MAIN 从另一个活动开始一个活动。我这样做是因为我想为其他应用程序提供启动此辅助活动的可能性。所以在我的清单中,我有两个主要活动:

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)

你能告诉我为什么找不到我的 Activity 或者我应该怎么做才能启动我的 SecondaryActivity

4

3 回答 3

2

你能试试这个吗?

Intent intent = new Intent(Intent.ACTION_MAIN);

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

第一个参数采用“应用程序包名称”

于 2013-03-28T15:56:24.290 回答
0

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

<intent-filter>
    <action android:name="main.package.secondary.SecondaryActivity" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
于 2013-03-28T15:53:12.160 回答
0

放入 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>

没有意图过滤器属性。

于 2013-03-28T16:20:24.307 回答