簡體   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