Android Beginer:java.lang.NullPointerException

Android Beginer:java.lang.NullPointerException,java,android,eclipse,android-layout,android-intent,Java,Android,Eclipse,Android Layout,Android Intent,我是android/java新手,请耐心听我说。我的代码在前面运行得很好,但自从我添加for()循环以来,我一直得到一个NullPointerException。有什么想法吗 public class PreferencesActivity extends Activity { SharedPreferences settings; SharedPreferences.Editor editor; static CheckBox box, box2; private final static

我是android/java新手,请耐心听我说。我的代码在前面运行得很好,但自从我添加for()循环以来,我一直得到一个NullPointerException。有什么想法吗

public class PreferencesActivity extends Activity {

SharedPreferences settings;
SharedPreferences.Editor editor;
static CheckBox box, box2;

private final static CheckBox[] boxes={box, box2};
private final static String[] checkValues={"checked1", "checked2"};


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    box=(CheckBox)findViewById(R.id.checkBox);
    box2=(CheckBox)findViewById(R.id.checkBox2);

    settings= getSharedPreferences("MyBoxes", 0);
    editor = settings.edit();

    if(settings != null){

    for(int i =0;i<boxes.length; i++)
        boxes[i].setChecked(settings.getBoolean(checkValues[i], false));   
    }

}

@Override
protected void onStop(){
   super.onStop();

   for(int i = 0; i <boxes.length; i++){           
   boolean checkBoxValue = boxes[i].isChecked();        
   editor.putBoolean(checkValues[i], checkBoxValue);
   editor.commit();       
   }

    }
}
公共类首选项活动扩展活动{
共享引用设置;
SharedReferences.Editor;
静态复选框,框2;
私有最终静态复选框[]框={box,box2};
私有最终静态字符串[]checkValues={“checked1”、“checked2”};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
box=(复选框)findViewById(R.id.CheckBox);
box2=(复选框)findViewById(R.id.checkBox2);
设置=GetSharedReferences(“MyBox”,0);
editor=settings.edit();
如果(设置!=null){

对于(int i=0;i您将
box
box2
的值初始化为
null
(因为这是未明确指定的默认值)。然后在创建
复选框
数组
时使用这些值。因此,
有两个空值。然后重新分配
框2
的值。请注意,这对
数组中的值没有影响。因此,当您尝试访问您得到的数组为
NullPointerException


框2
赋值后,在
中设置值。这是固定的代码。希望对您有所帮助:

public class PreferencesActivity extends Activity {

    SharedPreferences settings;
    SharedPreferences.Editor editor;
    static CheckBox box, box2; // box and box2 are null when class is loaded to DalvikVM

    private final static CheckBox[] boxes={null, null};
    private final static String[] checkValues={"checked1", "checked2"};


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        box=(CheckBox)findViewById(R.id.checkBox);
        box2=(CheckBox)findViewById(R.id.checkBox2);

        // assign CheckBox instances to boxes array
        boxes[0] = box;
        boxes[1] = box2;

        settings= getSharedPreferences("MyBoxes", 0);
        editor = settings.edit();

        if(settings != null){

        for(int i =0;i<boxes.length; i++)
            boxes[i].setChecked(settings.getBoolean(checkValues[i], false));   
        }

    }

    @Override
    protected void onStop() {
        super.onStop();

        for(int i = 0; i <boxes.length; i++){           
            boolean checkBoxValue = boxes[i].isChecked();        
            editor.putBoolean(checkValues[i], checkBoxValue);
            editor.commit();       
        }
    }
}
公共类首选项活动扩展活动{
共享引用设置;
SharedReferences.Editor;
静态复选框,box2;//将类加载到DalvikVM时,框和box2为null
私有最终静态复选框[]框={null,null};
私有最终静态字符串[]checkValues={“checked1”、“checked2”};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
box=(复选框)findViewById(R.id.CheckBox);
box2=(复选框)findViewById(R.id.checkBox2);
//将复选框实例指定给框数组
盒子[0]=盒子;
框[1]=框2;
设置=GetSharedReferences(“MyBox”,0);
editor=settings.edit();
如果(设置!=null){

对于(int i=0;ilogcat trace please),我认为for语句中的框是空的。您能举个例子说明我是如何做到这一点的吗?