我正在尝试使用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
答案 0 :(得分:2)
你可以尝试一下吗?
Intent intent = new Intent(Intent.ACTION_MAIN);
ComponentName cn = new ComponentName("main.package","main.package.secondary.SecondaryActivity");
intent.setComponent(cn);
startActivity(intent);
第一个参数采用“应用程序包名称”
答案 1 :(得分:0)
尝试将以下intent-filters设置为第二个活动:
<intent-filter>
<action android:name="main.package.secondary.SecondaryActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
答案 2 :(得分: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>
没有intent-filter属性。