我是 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();
}
}
}