实施onSaveInstanceState并将其自定义以在旋转屏幕后保存文本后,应用程序崩溃

时间:2019-02-03 22:18:37

标签: java android

我制作了一个应用,可在您选择最小和最大数字后向您显示随机数。但是旋转结果消失后,我实现了onSaveInstanceState并根据需要对其进行了自定义,此后我的应用始终崩溃。

公共类MainActivity扩展了AppCompatActivity {

EditText et_min,et_max;
Button button;
TextView tv_output;

Random r;
int min, max, output;
private static final String STATE_TV_OUTPUT = "tv_output";


@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    String onSave = savedInstanceState.getString("onSave");
}

@Override
protected void onSaveInstanceState(Bundle SavedInstanceState) {
    super.onSaveInstanceState(SavedInstanceState);
    SavedInstanceState.putString("onSave", "1");
}

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

    r = new Random();
    String onSave = tv_output.getText().toString();
    et_max = (EditText) findViewById(R.id.et_max);
    et_min = (EditText) findViewById(R.id.et_min);
    button = (Button) findViewById(R.id.button);
    tv_output = (TextView) findViewById(R.id.tv_output);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String tempMin, tempMax;
            tempMin = et_min.getText().toString();
            tempMax = et_max.getText().toString();
            if (!tempMin.equals("") && !tempMax.equals(""))//ovde stavljam ih u poziciju gde su slobodni za pisanje
                min = Integer.parseInt(tempMin);
                max = Integer.parseInt(tempMax);//ova dva sluze kako bi pretvorili stringove u intove za potrebe dole methoda
            if (max > min) {
                output = r.nextInt((max - min) + 1) + min;//nextInt sluzi kako bi dao random broj izmedju dva broja u ovom slucaju min i max
                tv_output.setText("" + output);
            }
        }

    });
}

}

3 个答案:

答案 0 :(得分:1)

您需要更改订单,否则更改的捆绑商品将不会通过:

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putString("onSave", "1");
    super.onSaveInstanceState(savedInstanceState);
}

super类的方法的调用并不总是最顶层的。

答案 1 :(得分:1)

logcat的输出是什么?

您可以在此处了解有关Android活动生命周期的更多信息:https://developer.android.com/guide/components/activities/activity-lifecycle#java

您保存和恢复状态的代码如下:

static final String STATE_KEY = "onSave";

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    String onSave = savedInstanceState.getString(STATE_KEY);
}

@Override
protected void onSaveInstanceState(Bundle SavedInstanceState) {
    SavedInstanceState.putString(STATE_KEY, "1");
    super.onSaveInstanceState(SavedInstanceState);//call this at the end
}

答案 2 :(得分:0)

放入此行

String onSave = tv_output.getText().toString();

tv_output初始化后,否则将获得NullPointerException。

更新:

要在旋转过程中将文本保存在TextView中,请将android:freezesText="true"添加到xml(What is (s/g)etFreezesText in TextView?)中,无需使用onSaveInstanceState。