今天在项目中碰到的一个错误:
项目中涉及到很多Activity之间相互跳转的代码,如下所示的代码,感觉很繁琐,因此想到了封装一下:
Intent intent = new Intent(SplashActivity.this , MainFragmentActivity.class);
startActivity(intent);
封装在了另一个类:UIHelper.java中
Intent intent = new Intent();
intent.setClass(context, cls);
context.startActivity(intent);
运行后,就报错了,错误log:
04-09 15:55:08.165: E/AndroidRuntime(3403): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
04-09 15:55:08.165: E/AndroidRuntime(3403): at android.app.ContextImpl.startActivity(ContextImpl.java:964)
04-09 15:55:08.165: E/AndroidRuntime(3403): at android.app.ContextImpl.startActivity(ContextImpl.java:951)
04-09 15:55:08.165: E/AndroidRuntime(3403): at android.content.ContextWrapper.startActivity(ContextWrapper.java:290)
04-09 15:55:08.165: E/AndroidRuntime(3403): at com.nfschina.sign.cover.UIHelper.startUserActivity(UIHelper.java:76)
04-09 15:55:08.165: E/AndroidRuntime(3403): at com.nfschina.sign.cover.activity.SettingActivity.onPreferenceClick(SettingActivity.java:125)
04-09 15:55:08.165: E/AndroidRuntime(3403): at android.preference.Preference.performClick(Preference.java:952)
04-09 15:55:08.165: E/AndroidRuntime(3403): at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:215)
04-09 15:55:08.165: E/AndroidRuntime(3403): at android.widget.AdapterView.performItemClick(AdapterView.java:315)
04-09 15:55:08.165: E/AndroidRuntime(3403): at android.widget.AbsListView.performItemClick(AbsListView.java:1107)
04-09 15:55:08.165: E/AndroidRuntime(3403): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2795)
04-09 15:55:08.165: E/AndroidRuntime(3403): at android.widget.AbsListView$1.run(AbsListView.java:3470)
04-09 15:55:08.165: E/AndroidRuntime(3403): at android.os.Handler.handleCallback(Handler.java:730)
04-09 15:55:08.165: E/AndroidRuntime(3403): at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 15:55:08.165: E/AndroidRuntime(3403): at android.os.Looper.loop(Looper.java:137)
04-09 15:55:08.165: E/AndroidRuntime(3403): at android.app.ActivityThread.main(ActivityThread.java:5136)
04-09 15:55:08.165: E/AndroidRuntime(3403): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 15:55:08.165: E/AndroidRuntime(3403): at java.lang.reflect.Method.invoke(Method.java:525)
04-09 15:55:08.165: E/AndroidRuntime(3403): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-09 15:55:08.165: E/AndroidRuntime(3403): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-09 15:55:08.165: E/AndroidRuntime(3403): at dalvik.system.NativeStart.main(Native Method)
第一行提示:
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
错误分析:
Context中有一个startActivity方法,Activity继承自Context,重载了startActivity方法。如果使用Activity的startActivity方法,不会有任何限制,而如果使用Context的startActivity方法的話,就需要开启一个新的的task,遇到这个异常,是因为使用了Context的startActivity方法。解决办法是,加一个flag。
代码:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
这样就可以在新的task里面启动这个Activity了。
/**
* 启动新的Activity
* @param context 当前Activity
* @param cls 要启动的Activity的类
*/
public static void startUserActivity(Context context , Class cls){
Intent intent = new Intent();
intent.setClass(context, cls);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
context.startActivity(intent);
}