gpt4 book ai didi

java - Android SDK SharedPreferences 的麻烦

转载 作者:行者123 更新时间:2023-12-01 14:35:53 26 4
gpt4 key购买 nike

我正在开发一个应用程序,并且想要保存我的复选框选项的 obe 状态,但是当我尝试使用 SharedPreferences 类(第一次使用它)时,我收到一个空指针异常。我已经排查了几个小时的故障,但找不到解决方案。谁能看看这个并告诉我出了什么问题吗?完整的代码要长得多,但这是给我空指针异常的公园。我知道它与 SharedPreferences 有关。

package us.mattmccoy.meanfind;

//import java.util.Arrays;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {


//saved data stuff needed
SharedPreferences preferences = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
Editor edit = preferences.edit();
//private data using saved data
String autoclearKEY = "us.mattmccoy.meanfind.AUTOCLEAR";
boolean autoclear = preferences.getBoolean(autoclearKEY, false);

//normal private data
final Context con1 = this;
int dividend;String dataFixed;boolean tb;
private CheckBox myCBox, acBox;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerBoxes();
if(autoclear == true){
acBox.setChecked(true);
}else{
acBox.setChecked(false);
}
}
//check box listeners
public void addListenerBoxes(){
//instantiate boxes
acBox = (CheckBox) findViewById(R.id.chex2);
acBox.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//is acBox checked?
if (((CheckBox) v).isChecked()) {
edit.putBoolean(autoclearKEY, true).commit();
}else{
edit.putBoolean(autoclearKEY, false).commit();
}
}
});
myCBox = (CheckBox) findViewById(R.id.chex);

}

错误信息:

http://pastebin.com/5P2Mfwik

最佳答案

更改此行

SharedPreferences preferences = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE); 

SharedPreferences preferences; = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE); 

并放置

preferences = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);

onCreate()

您在使用 onCreate() 建立 Context 之前尝试使用它,因此您在 context 上得到 NPE >。您还需要移动这些行

Editor edit = preferences.edit();
boolean autoclear = preferences.getBoolean(autoclearKEY, false);

在初始化preferences后进入onCreate(),否则您将得到另一个NPE,因为preferences将是null 直到初始化它。所以应该是这样的

public class MainActivity extends Activity {
//saved data stuff needed
SharedPreferences preferences; //declare them
Editor edit;
//private data using saved data
String autoclearKEY = "us.mattmccoy.meanfind.AUTOCLEAR";
boolean autoclear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences preferences; = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
Editor edit = preferences.edit(); //initialize them
boolean autoclear = preferences.getBoolean(autoclearKEY, false);
addListenerBoxes();
if(autoclear == true){
acBox.setChecked(true);
}else{
acBox.setChecked(false);
}

如果这不能解决您的问题,请发布完整的 logcat

关于java - Android SDK SharedPreferences 的麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16492152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com