通过intent启动活动时出错

时间:2012-03-26 12:00:33

标签: android android-intent

我正在尝试使用以下代码启动活动:

Intent i = new Intent();
i.setClassName("com.android.launcher2", "com.android.launcher2.Launcher");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

上面的代码我得到以下异常:

01-01 00:05:03.617: ERROR/AndroidRuntime(1458): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.LOCALE_CHANGED flg=0x30 } in com.android.launcher2.LauncherModel@4194bcc0
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     at   android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:737)
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     at android.os.Handler.handleCallback(Handler.java:605)
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     at android.os.Looper.loop(Looper.java:137)
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     at android.app.ActivityThread.main(ActivityThread.java:4368)
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     at java.lang.reflect.Method.invokeNative(Native Method)
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     at java.lang.reflect.Method.invoke(Method.java:511)
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     at dalvik.system.NativeStart.main(Native Method)
01-01 00:05:03.617: ERROR/AndroidRuntime(1458): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.launcher2/com.android.launcher2.Launcher}; have you declared this activity in your AndroidManifest.xml?
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     at android.app.ContextImpl.startActivity(ContextImpl.java:889)
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     at com.android.launcher2.LauncherModel.onReceive(LauncherModel.java:634)
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
01-01 00:05:03.617: ERROR/AndroidRuntime(1458):     ... 9 more

我正在尝试从同一个包中的一个类开始活动 - com.android.launcher2,并且我在清单中定义了此活动(Launcher)。

有人可以告诉我导致此错误的原因,但一切看起来都不错

修改

Intent i = new Intent(getApplicationContext(), com.android.launcher2.Launcher.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

我已经做了这个并经过测试。现在,活动按以下顺序恢复 - onNewIntent()onResume()。相反,我需要onDestroy()onCreate()序列发生。我怎么能做到这一点?任何人在这方面的任何帮助都非常感谢

8 个答案:

答案 0 :(得分:3)

按照:


在Java中

    Intent i = new Intent();
    i.setClassName("com.android.launcher2", "com.android.launcher2.Launcher");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);

在清单

        <activity android:name="com.android.launcher2.Launcher" class="com.android.launcher2.Launcher">
        </activity>

答案 1 :(得分:1)

您是否在onStop()中取消注册LauncherModel中的所有广播接收器?

答案 2 :(得分:1)

尝试将.class添加到您的班级名称。

答案 3 :(得分:1)

试试这个:

        Intent i = new Intent(getApplicationContext(), com.android.launcher2.Launcher.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);

答案 4 :(得分:1)

未找到类的异常意味着......要么您没有在清单文件中定义该类,要么您正在明确地创建该类。请检查清单文件。

在你的活动中做到 -

   Intent intent = new Intent(act1.this, act2.class);
   startActivity(intent);

答案 5 :(得分:1)

如果您的设备管理多个帐户并且该应用程序已安装在当前会话以外的帐户中,则可能会发生这种情况。

您将必须在所有帐户上卸载该应用程序,然后在当前帐户会话中重新安装它。

答案 6 :(得分:0)

除了你的编辑:看看你的意图标志我认为有问题看看android dev他们明确地说这个标志经常与多个标志组合。

答案 7 :(得分:0)

我的情况:

之前的Android清单:

mov rax, cs:jumpAddrAbsolute
mov rax, rcx
mov r11, rax
jmp r11

之后的Android清单:

<activity android:name=".modules.login.LoginActivity" />

上述更改有助于在运行时重定向到参考,并解决了此问题。

相关问题