【问题标题】:How to store a default value in SharedPreferences如何在 SharedPreferences 中存储默认值
【发布时间】:2018-10-07 13:06:22
【问题描述】:

我正在创建一个餐厅应用程序,其中包括注册/登录系统的成员和通过跳过注册/登录过程查看菜单的非成员。 我的主屏幕有菜单(非会员直接查看)、登录注册按钮(会员注册/登录)。 我想将默认的 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");
}


}

MainActivity.java

public class MainActivity extends AppCompatActivity {

Button btnSignIn, btnSignUp, btnMenu;
public AppPreferences appPreference;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnMenu = (Button)findViewById(R.id.btnMenu);
    btnSignUp = (Button)findViewById(R.id.btnSignUp);
    btnSignIn = (Button)findViewById(R.id.btnSignIn);



    btnMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent home = new Intent(MainActivity.this, Home.class);
            //Here save user info to preferences
            appPreference.setUserPhoneId(Constant.DEFAULT_PHONE_ID);
            appPreference.setUserPassword(Constant.DEFAULT_PASSWORD);
            startActivity(home);


        }
    });

    btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent signUp = new Intent(MainActivity.this, SignUp.class);
            startActivity(signUp);
        }
    });

    btnSignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent signIn = new Intent(MainActivity.this, SignIn.class);
            startActivity(signIn);
        }
    });
}
}

我需要跟踪非会员的交易订单,所以我需要将所有非会员设置为默认的 phoneId 和密码。

【问题讨论】:

  • SharedPreferenes 方法接受一个键和一个值,而不是一个值和一个键。你的论点被颠倒了。
  • @TheWanderer 是改成editor.putString(userId, USER_PHONE)editor.putString(userPassword, USER_PASSWORD) 了吗?

标签: java android sharedpreferences autologin


【解决方案1】:

您从未在 MainActivity 中初始化 appPreference

onCreate() 下执行此操作:

appPreference = new AppPreferences(this);

【讨论】:

  • 我添加了appPreference = new AppPreferences(this); 这一行,但是出现红线错误AppPreferences 有私有访问权限,我需要将AppPreferences.java 中的private AppPreferences(Context context) 更改为public AppPreferences(Context context) ?
  • 是的。构造函数必须是公开的。
  • 好的,我可以再问一个问题吗?共享首选项是否仅将默认值存储在应用程序本身上?我如何在 firebase 中保存默认的 phoneId 和密码,因为我需要跟踪他们的交易订单
  • @Blue77 getString 或 getInt 方法的第二个参数是您指定默认值的位置。
  • 如何将共享首选项中的默认值同步到 Firebase?可能吗?我可能想从 firebase 的默认值中检索数据
【解决方案2】:

您需要在访问其方法和默认值之前初始化应用程序首选项对象,如果未找到键值,您可以从首选项返回。

 sharedPreferences.getString(USER_PHONE,"default_phone");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    相关资源
    最近更新 更多