Java 错误标志\u活动\u新建\u任务标志
您好,我尝试构建一个应用程序,但在我注销应用程序时出现下一个错误,我的应用程序使用登录用户,但按“注销”时显示以下内容: 错误Java 错误标志\u活动\u新建\u任务标志,java,android,android-activity,Java,Android,Android Activity,您好,我尝试构建一个应用程序,但在我注销应用程序时出现下一个错误,我的应用程序使用登录用户,但按“注销”时显示以下内容: 错误 android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 注销代码 public v
android.util.AndroidRuntimeException: Calling startActivity() from
outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK
flag. Is this really what you want?
注销代码
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));
}
这是因为您试图使用非活动上下文
mCtx
启动活动。您需要使用logout
方法所在的活动,如下所示:
public void logout() {
...
startActivity(new Intent(this, LoginActivity.class));
// or use YourActivity.this instead of this
}
嗨,当我按logout时,执行以下操作:
finish();SharedPrefManager.getInstance(getApplicationContext()).logout()代码>您可以尝试使用Intent Intent=newintent(mCtx,LoginActivity.class);intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK | intent.FLAG_ACTIVITY_CLEAR_TOP);mCtx.startActivity(intent)
在任务中只进行登录活动。您的logout()
code是活动类的一部分还是其他类的一部分?