7

我试图在我的课堂上开始Activity和关闭另一个( )。AsyncTaskonPostExecute()

我的代码:

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();

但没有异常结果(仍然是相同的错误)。

有什么办法可以正确吗?

4

3 回答 3

22

logcat 在第一行告诉你问题出在哪里

Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag

添加该标志

Intent i = new Intent(context, ThunderHunter.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.startActivity(i);
parentActivity.finish();

您可以在文档中获取所有可用的列表Intent Flags

于 2013-08-01T16:12:00.423 回答
3

添加Intent.FLAG_ACTIVITY_NEW_TASK将解决您的错误,但请确保您是否需要此标志,因为它将触发活动作为您的场景中可能不想要的新任务。

In order to avoid this flag, you can write a Handler in main Activity and call it in onPostExecute()

于 2013-08-02T02:03:22.320 回答
2

试试这个-

Intent i = new Intent(context, ThunderHunter.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
parentActivity.finish();

您在活动上下文之外调用新活动,因此您必须设置标志并将上下文传递到活动之外。

于 2013-08-01T16:10:44.927 回答