在“活动"上下文之外启动新的“活动". [英] Start new Activity outside the Activity context.
本文介绍了在“活动"上下文之外启动新的“活动".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试启动 Activity
并在我的 AsyncTask
类( onPostExecute()
)中关闭其他项目.
I tried to start an Activity
and close other in my AsyncTask
class (onPostExecute()
).
我的代码:
Intent i = new Intent(parentActivity, ThunderHunter.class);
c.startActivity(i);
parentActivity.finish();
但是它不起作用,logcat显示:
But it doesn't work, logcat shows :
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();
但是没有例外的结果(仍然是相同的错误).
But without excepted results (still same error).
有什么办法可以解决这个问题吗?
Is there any way to that properly ?
推荐答案
logcat告诉您第一行出现了什么问题
The logcat tells you what the problem is in the first line
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
此处的列表
You can get a list of all available Intent Flags
here in the docs
这篇关于在“活动"上下文之外启动新的“活动".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文