【问题标题】:Android SDK SharedPreferences troublesAndroid SDK SharedPreferences 的烦恼
【发布时间】:2013-05-10 23:25:46
【问题描述】:

我正在开发一个应用程序并希望保存我的复选框选项的 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

【问题讨论】:

    标签: java android nullpointerexception sharedpreferences


    【解决方案1】:

    改变这一行

    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

    【讨论】:

    • 你看到我更新的答案了吗?如果您仍然收到错误,请发布完整的 logcat
    • 当我将编辑器对象编辑移动到 onCreate() 时,我的类中的其他方法无法访问它。为什么会这样/我该如何规避它?
    • 您可以将其声明为成员变量(在onCreate() Editor edit; 之前,但您必须在onCreate() 中初始化您的preferences 之后才能对其进行初始化
    • 我做了另一个编辑,可能会更清楚。另外,据我所知,您不需要final Context con1 = this;。如果您使用Activity 方法,则可以使用this,否则可以使用MainActivity.this
    • 我不小心让我的模拟器崩溃了,但它看起来很有效!即使在应用程序关闭后,这是否会保存首选项?编辑:是的,con1 是我需要清理的剩余代码。
    猜你喜欢
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    相关资源
    最近更新 更多