在活动之间保存微调器的状态

时间:2017-03-01 20:17:55

标签: java android

我对Android很新,但在保存我的活动状态方面遇到了问题,特别是我的微调器,因此他们保留了它们的价值。

我已尝试过saveinstancestate和偏好设置,我仍然无法工作,只是想知道是否有人可以查看我的代码并查看我出错的地方。

public class YearOneActivity extends AppCompatActivity {

Button ButtonSubmit;
Spinner SpinnerGradesUnitOne, SpinnerGradesUnitTwo, SpinnerGradesUnitThree, SpinnerGradesUnitSix, SpinnerGradesUnitNine, SpinnerGradesUnitTwentySix, SpinnerGradesUnitEighteen, SpinnerGradesUnitTwentyEight, SpinnerGradesUnitFortyThree;
int spinnerUnit1;

public void SubmitMainActivity(){
    ButtonSubmit = (Button)findViewById(R.id.button_submit);
    ButtonSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent SubmitMainActivity = new Intent(YearOneActivity.this, MainActivity.class);

            startActivity(SubmitMainActivity);
        }
    });
}

public void SpinnerGrades(){
    SpinnerGradesUnitOne = (Spinner) findViewById(R.id.spinner_grades_unit_one);
    SpinnerGradesUnitTwo = (Spinner) findViewById(R.id.spinner_grades_unit_two);
    SpinnerGradesUnitThree = (Spinner) findViewById(R.id.spinner_grades_unit_three);
    SpinnerGradesUnitSix = (Spinner) findViewById(R.id.spinner_grades_unit_six);
    SpinnerGradesUnitNine = (Spinner) findViewById(R.id.spinner_grades_unit_nine);
    SpinnerGradesUnitTwentySix = (Spinner) findViewById(R.id.spinner_grades_unit_twentysix);
    SpinnerGradesUnitEighteen = (Spinner) findViewById(R.id.spinner_grades_unit_eighteen);
    SpinnerGradesUnitTwentyEight = (Spinner) findViewById(R.id.spinner_grades_unit_twentyeight);
    SpinnerGradesUnitFortyThree = (Spinner) findViewById(R.id.spinner_grades_unit_fortythree);

    String[] spinnerArrayGrades = getResources().getStringArray(R.array.spinner_grades);
    ArrayAdapter<String> adapterGrades = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArrayGrades);

    SpinnerGradesUnitOne.setAdapter(adapterGrades);

    SpinnerGradesUnitTwo.setAdapter(adapterGrades);
    SpinnerGradesUnitThree.setAdapter(adapterGrades);
    SpinnerGradesUnitSix.setAdapter(adapterGrades);
    SpinnerGradesUnitNine.setAdapter(adapterGrades);
    SpinnerGradesUnitTwentySix.setAdapter(adapterGrades);
    SpinnerGradesUnitEighteen.setAdapter(adapterGrades);
    SpinnerGradesUnitTwentyEight.setAdapter(adapterGrades);
    SpinnerGradesUnitFortyThree.setAdapter(adapterGrades);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_year_one);

    if (savedInstanceState != null){

        SpinnerGradesUnitOne.setSelection(savedInstanceState.getInt("spinnerUnit1", 0));
        SpinnerGradesUnitTwo.setSelection(savedInstanceState.getInt("spinnerUnit2", 0));
        SpinnerGradesUnitThree.setSelection(savedInstanceState.getInt("spinnerUnit3", 0));
        SpinnerGradesUnitSix.setSelection(savedInstanceState.getInt("spinnerUnit6", 0));
        SpinnerGradesUnitNine.setSelection(savedInstanceState.getInt("spinnerUnit9", 0));
        SpinnerGradesUnitTwentySix.setSelection(savedInstanceState.getInt("spinnerUnit26", 0));
        SpinnerGradesUnitEighteen.setSelection(savedInstanceState.getInt("spinnerUnit18", 0));
        SpinnerGradesUnitTwentyEight.setSelection(savedInstanceState.getInt("spinnerUnit28", 0));
        SpinnerGradesUnitFortyThree.setSelection(savedInstanceState.getInt("spinnerUnit43", 0));

    }

    SubmitMainActivity();
    SpinnerGrades();

}

@Override
public  void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putInt("spinnerUnit1", SpinnerGradesUnitOne.getSelectedItemPosition());
    outState.putInt("spinnerUnit2", SpinnerGradesUnitTwo.getSelectedItemPosition());
    outState.putInt("spinnerUnit3", SpinnerGradesUnitThree.getSelectedItemPosition());
    outState.putInt("spinnerUnit6", SpinnerGradesUnitSix.getSelectedItemPosition());
    outState.putInt("spinnerUnit9", SpinnerGradesUnitNine.getSelectedItemPosition());
    outState.putInt("spinnerUnit26", SpinnerGradesUnitTwentySix.getSelectedItemPosition());
    outState.putInt("spinnerUnit18", SpinnerGradesUnitEighteen.getSelectedItemPosition());
    outState.putInt("spinnerUnit28", SpinnerGradesUnitTwentyEight.getSelectedItemPosition());
    outState.putInt("spinnerUnit43", SpinnerGradesUnitFortyThree.getSelectedItemPosition());

}

1 个答案:

答案 0 :(得分:0)

我认为Spinners会自动保持状态,当你在布局xml中为他们提供一个独特的时候。如果不是这样的话,你应该像使用onSaveInstanceState一样。 如果您的代码如上所示,我绝对想知道为什么您没有收到NullpointerException。即使您已在新活动中初始化它们,此时您仍然会设置旧Spinner的状态。因此,尝试在if-block之前移动最后一个方法调用。然后它应该工作:

@Override
protected void onCreate(Bundle  savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_year_one);

    SubmitMainActivity();
    SpinnerGrades();

    if (savedInstanceState != null) {
        // ...
    }

}