context.getSharedPreferences(string name,int mode) 空指针异常

本文探讨了在Android开发中使用SharedPreferences时遇到的空指针异常问题,并详细解释了异常出现的原因及如何避免该问题的发生。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public SharedPreferenceUtils(Context context,String name) {
    LogUtil.i("Test",context.toString());
    mSharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
    mEditor = mSharedPreferences.edit();
}
工具类里获取ShardPreferences对象时报错:java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.fuyong.smartbutler/com.fuyong.smartbutler.ui.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
04-23 16:07:50.212 189com.fuyong.smartbutler I/Test: com.fuyong.smartbutler.ui.LoginActivity@999a96a

尝试Log打印context确认context对象已经传进来,按理不会空指针。

报错时我是在Activity的成员变量位置调用这个工具类的构造方法:

private SharedPreferenceUtils mSpUser = mSpUser = new SharedPreferenceUtils(this, FILE_USER);

当我试着把它放到onCreate方法里之后Bug解决,原因是在成员变量位置传入“this"时,Activity还未构建出来自然不能当作上下文使用

mSpUser = new SharedPreferenceUtils(this, FILE_USER);
private Button login;// 登录按钮 private CheckBox rememberPsdBox, autoLoginBox;// 记住密码、自动登陆复选框 private EditText name, psd;// 用户名和密码 private TextView userInfo; SharedPreferences loginPreferences, accessPreferences;// 保存登陆信息和访问次数 SharedPreferences.Editor loginEditor, accessEditor;// 对应的编辑器 String userName; String userPsd; boolean isSavePsd,isAutoLogin; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); loginPreferences = this.getSharedPreferences("login", Context.MODE_PRIVATE); //补充完整-1:采用参数"access"和Context.MODE_PRIVATE,实例化accessPreferences对象 int count=accessPreferences.getInt("count",1); Toast.makeText(MainActivity.this,"欢迎您,这是第"+count+"次访问!", Toast.LENGTH_LONG).show(); loginEditor = loginPreferences.edit(); //补充完整-2:从accessPreferences中,实例化accessEditor编辑器 accessEditor.putInt("count",++count); accessEditor.commit(); //提交 userName = loginPreferences.getString("name", null); userPsd = loginPreferences.getString("psd", null); isSavePsd=loginPreferences.getBoolean("isSavePsd",false); isAutoLogin=loginPreferences.getBoolean("isAutoLogin", false); if (isAutoLogin) { this.setContentView(R.layout.activity_welcome); userInfo = (TextView) findViewById(R.id.userInfo); userInfo.setText("欢迎您:" + userName + ",登陆成功!"); } else{ loadActivity(); } } public void loadActivity() { this.setContentView(R.layout.activity_main); login = (Button) findViewById(R.id.login); rememberPsdBox = (CheckBox) findViewById(R.id.rememberPsd); autoLoginBox = (CheckBox) findViewById(R.id.autoLogin); name = (EditText) findViewById(R.id.name); psd = (EditText) findViewById(R.id.psd); if (isSavePsd) { psd.setText(userPsd); name.setText(userName); rememberPsdBox.setChecked(true); } login.setOnClickListener(new OnClickListener() { public void onClick(View v) { loginEditor.putString("name", name.getText().toString()); loginEditor.putString("psd", psd.getText().toString()); loginEditor.putBoolean("isSavePsd", rememberPsdBox.isChecked()); loginEditor.putBoolean("isAutoLogin", autoLoginBox.isChecked()); //补充完整-3:loginEditor的commit方法提交数据 MainActivity.this.setContentView(R.layout.activity_welcome); userInfo = (TextView) findViewById(R.id.userInfo); userInfo.setText("欢迎您:" + name.getText().toString() + ",登陆成功!"); } }); }补充代码
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值