Android 调用startActivity()需要标记\u活动\u新任务
我正在使用SharedPreferences获取用户/注销用户,但我不断收到错误“从活动上下文外部调用startActivity()需要标志\u Activity\u NEW\u TASK标志”。注销方法用于清除设备中存储的数据 这是我使用的注销方法:Android 调用startActivity()需要标记\u活动\u新任务,android,android-context,Android,Android Context,我正在使用SharedPreferences获取用户/注销用户,但我不断收到错误“从活动上下文外部调用startActivity()需要标志\u Activity\u NEW\u TASK标志”。注销方法用于清除设备中存储的数据 这是我使用的注销方法: logout_nav.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
logout_nav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPrefManager.getInstance(getApplicationContext()).logout();
}
我的共享偏好代码是
public class SharedPrefManager {
private static final String SHARED_PREF_NAME = "volleyregisterlogin";
private static final String KEY_PHONENUMBER = "keyphonenumber";
private static final String KEY_LASTNAME = "keylastname";
private static final String KEY_FULLNAME = "keyfullname";
private static final String KEY_MIDDLEINITIAL = "keymiddleinitial";
private static final String KEY_COUNTRY = "keycountry";
private static final String KEY_ADDRESS = "keyaddress";;
private static final String KEY_ID = "keyid";
private static final String KEY_BALANCE = "keybalance";
private static SharedPrefManager mInstance;
private static Context ctx;
private SharedPrefManager(Context context) {
ctx = context;
}
public static synchronized SharedPrefManager getInstance(Context context) {
if (mInstance == null) {
mInstance = new SharedPrefManager(context);
}
return mInstance;
}
//this method will store the user data in shared preferences
public void userLogin(User user) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(KEY_PHONENUMBER, user.getPhone_number());
editor.putString(KEY_LASTNAME, user.getLastname());
editor.putString(KEY_FULLNAME, user.getFullname());
editor.putString(KEY_MIDDLEINITIAL, user.getMiddleinitial());
editor.putString(KEY_COUNTRY, user.getCountry());
editor.putString(KEY_ADDRESS, user.getAddress());
editor.putFloat(KEY_BALANCE, (float) user.getBalance());
editor.apply();
}
//this method will checker whether user is already logged in or not
public boolean isLoggedIn() {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(KEY_PHONENUMBER, null) != null;
}
//this method will give the logged in user
public User getUser() {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return new User(
sharedPreferences.getString(KEY_PHONENUMBER, null),
sharedPreferences.getString(KEY_LASTNAME, null),
sharedPreferences.getString(KEY_FULLNAME, null),
sharedPreferences.getString(KEY_MIDDLEINITIAL, null),
sharedPreferences.getString(KEY_COUNTRY, null),
sharedPreferences.getString(KEY_ADDRESS, null),
sharedPreferences.getFloat(KEY_BALANCE, 0)
);
}
//this method will logout the user
public void logout() {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
ctx.startActivity(new Intent(ctx.getApplicationContext(), Login.class));
}
}
您传递给SharedPrefManager的ctx是ApplicationContext,不允许它启动活动。所以
logout_nav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPrefManager.getInstance(xxxActivity.this).logout();
}
那太好了。将xxx活动更改为您自己的活动。在发布前不久已经尝试过这样做,但我仍然收到相同的错误。因此,我认为您将删除单实例模式,我认为您将在其他地方使用ApplicationContext初始化管理器。您的问题与
共享引用
无关。这个标题有误导性。您的问题是在非活动
上下文上调用startActivity()
(在本例中可能是ApplicationContext
)。