我是新來的android/java,請耐心等待。我的代碼工作得很好,但自從我添加for()循環以來,我一直在收到一個NullPointerException。有任何想法嗎?Android開發者:java.lang.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();
}
}
}
logcat的痕跡請,我認爲這箱子是空您的發言 – moujib