我正在创建一个餐厅应用程序,该应用程序由注册/登录系统的会员和非会员通过跳过注册/登录过程来查看菜单组成。我的主屏幕有菜单(非会员直接查看)、登录和注册按钮(会员注册/登录)。我想将默认的 phoneId 和密码存储到 SharedPreference 中,让“非会员”通过跳过登录/注册过程(p/s仅非会员自动登录)来查看菜单。但是,我总是面临 nullPointerException 错误,并且无法让非会员在按下菜单按钮时查看。我不知道哪里错了,因为我只是一个初学者和学习过程。
常量.java
public class Constant {
public static final String DEFAULT_PHONE_ID = "9876543210";
public static final String DEFAULT_PASSWORD = "12345";
}
AppPreferences.java
public class AppPreferences {
// Class variables
private Context context;
private static SharedPreferences sharedPreferences;
private static SharedPreferences.Editor editor;
public static final String PREF_NAME = "iMenuApp";
private int PRIVATE_MODE = 0;
// Define your preferences key
private static final String USER_PHONE = "9876543210";
private static final String USER_PASSWORD = "12345";
private AppPreferences(Context context)
{
this.context = context;
sharedPreferences = this.context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
if (sharedPreferences != null)
{
editor = sharedPreferences.edit();
}
}
//Store user PhoneId
public void setUserPhoneId(String userId){
String TAG = "AppPref:setUserId";
try
{
editor.putString(USER_PHONE, userId);
editor.commit();
} catch (Exception e) {
Log.e(TAG, String.valueOf(e));
}
}
// Get userPhoneId
public String getUserPhoneId(){
return sharedPreferences.getString(USER_PHONE,"default_phone");
}
//Store userPassword
public void setUserPassword(String userPassword){
String TAG = "AppPref:setUserPassword";
try
{
editor.putString(USER_PASSWORD, userPassword);
editor.commit();
} catch (Exception e) {
Log.e(TAG, String.valueOf(e));
}
}
// Get userPassword
public String getUserPassword(){
return sharedPreferences.getString(USER_PASSWORD,"default_password");
}
}
我需要跟踪非会员的交易订单,所以我需要在默认的 phoneId 和密码下设置所有非会员。
拉风的咖菲猫
慕桂英4014372
相关分类