如何在SharedPreferences中存储默认值

时间:2018-10-07 13:06:22

标签: java android sharedpreferences autologin

我正在创建一个餐厅应用程序,该应用程序由跳过注册/登录过程的成员组成,用以注册/登录到系统,而非成员组成的用户查看菜单。 我的主屏幕具有菜单(供非会员直接查看),登录注册按钮(供会员注册/登录) )。 我想将默认的phoneId和密码存储到SharedPreference中,以使“非会员”通过跳过登录/注册过程来查看菜单(p / s 仅非会员自动登录)。但是,我始终会遇到nullPointerException错误,并且在非成员按下菜单按钮时无法让他们查看。我不知道哪里出了问题,因为我只是一个初学者和学习过程。

Constant.java

class ViewController: UIViewController {
    //I want to be able to initialize MyUIButton like below
    @IBOutlet weak var button11: MyUIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        //initialize the databaseAccessor class
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext
        DatabaseAccessor.shared = DatabaseAccessor(delegate: appDelegate, viewContext: context)

        // the code that I want to work is below
        button11.getLocation()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


import UIKit

@IBDesignable
class MyUIButton: UIButton {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    @IBInspectable var purposeName = ""
    private var purposeIcon = ""
    private var location = 0

    func setPurposeName(name: String){
        purposeName = name
    }

    func getPurposeName()-> String{
        return purposeName
    }

    func setPurposeIcon(name: String){
        purposeIcon = name
    }

    func getPurposeIcon()-> String{
        return purposeIcon
    }

    func setLocation(location: Int){
        self.location = location
    }

    func getLocation()-> Int{
        return location
    }
}

AppPreferences.java

public class Constant {
public static final String DEFAULT_PHONE_ID = "9876543210";
public static final String DEFAULT_PASSWORD = "12345";
}

MainActivity.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和密码。

2 个答案:

答案 0 :(得分:0)

您从未在MainActivity中初始化appPreference

onCreate()下执行此操作:

appPreference = new AppPreferences(this);

答案 1 :(得分:0)

您需要先初始化应用程序首选项对象,然后才能访问它的方法和默认值(如果找不到关键值,则可以从首选项返回)。

 sharedPreferences.getString(USER_PHONE,"default_phone");