[英]error FLAG_ACTIVITY_NEW_TASK flag
Hi I try to build an app, but I have the next error when I logout app, my app use login users, but when press logout show me this: 嗨,我尝试构建一个应用程序,但是注销应用程序时出现下一个错误,我的应用程序使用登录用户,但是按注销时,向我显示以下内容:
error 错误
android.util.AndroidRuntimeException: Calling startActivity() from
outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK
flag. Is this really what you want?
logout code 注销代码
public void logout() {
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
mCtx.startActivity(new Intent(mCtx, LoginActivity.class));
}
This is because you're trying to start the activity using mCtx
which is a non-activity context. 这是因为您正在尝试使用mCtx
(一个非活动上下文)来启动活动。 You need to use the Activity where logout
method reside, something like this: 您需要使用logout
方法所在的Activity,如下所示:
public void logout() {
...
startActivity(new Intent(this, LoginActivity.class));
// or use YourActivity.this instead of this
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.