繁体   English   中英

通过意图启动活动时出错

[英]Error while launching activity through 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()序列发生。 我怎样才能做到这一点? 非常感谢任何人在这方面的任何帮助

按照这个:


在 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>

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

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

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

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

尝试这个:

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

未找到类异常意味着......要么你没有在你的清单文件中定义这个类,要么你明确地创建了这个类......所以请检查你的清单文件。

在你的活动中做到这一点——

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

我的情况:

之前的 Android 清单:

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

Android 清单之后:

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

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

除了你的编辑:看看你的意图标志我认为有问题看看他们明确说这个标志通常与多个标志结合的android开发。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM