我試圖啓動Activity
並關閉其中的AsyncTask
類(onPostExecute()
)。在活動上下文之外開始新的活動。
我的代碼:
Intent i = new Intent(parentActivity, ThunderHunter.class);
c.startActivity(i);
parentActivity.finish();
但它不工作,logcat中顯示:
08-01 18:01:27.640: E/AndroidRuntime(12398): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
08-01 18:01:27.640: E/AndroidRuntime(12398): at android.app.ContextImpl.startActivity(ContextImpl.java:1029)
08-01 18:01:27.640: E/AndroidRuntime(12398): at android.app.ContextImpl.startActivity(ContextImpl.java:1023)
08-01 18:01:27.640: E/AndroidRuntime(12398): at android.content.ContextWrapper.startActivity(ContextWrapper.java:283)
08-01 18:01:27.640: E/AndroidRuntime(12398): at com.radzik.thunter.FunkcjeAPI$Logowanie.onPostExecute(FunkcjeAPI.java:151)
08-01 18:01:27.640: E/AndroidRuntime(12398): at com.radzik.thunter.FunkcjeAPI$Logowanie.onPostExecute(FunkcjeAPI.java:1)
08-01 18:01:27.640: E/AndroidRuntime(12398): at android.os.AsyncTask.finish(AsyncTask.java:631)
08-01 18:01:27.640: E/AndroidRuntime(12398): at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-01 18:01:27.640: E/AndroidRuntime(12398): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
08-01 18:01:27.640: E/AndroidRuntime(12398): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 18:01:27.640: E/AndroidRuntime(12398): at android.os.Looper.loop(Looper.java:137)
08-01 18:01:27.640: E/AndroidRuntime(12398): at android.app.ActivityThread.main(ActivityThread.java:4898)
08-01 18:01:27.640: E/AndroidRuntime(12398): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 18:01:27.640: E/AndroidRuntime(12398): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 18:01:27.640: E/AndroidRuntime(12398): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
08-01 18:01:27.640: E/AndroidRuntime(12398): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
08-01 18:01:27.640: E/AndroidRuntime(12398): at dalvik.system.NativeStart.main(Native Method)
所以我改變的代碼是:
Intent i = new Intent(context, ThunderHunter.class);
c.startActivity(i);
parentActivity.finish();
但是,如果沒有例外的結果(仍然是相同的錯誤)。
有什麼辦法可以正確使用嗎?
方面從至極活動意味着你要撥打的新的,所以試試這個意向書I =新意圖(YourActivity .this(或getApplicationContext()),ThunderHunter.class); – Tobiel