java应用程序在实现onsaveinstancestate并自定义它以在旋转屏幕后保存文本后崩溃

jtw3ybtb  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(243)

我做了一个应用程序,显示你随机数后,你选择了最小值和最大值。但是在旋转结果消失之后,我实现了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);
            }
        }

    });
}

}

xghobddn

xghobddn1#

什么是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
}
wqsoz72f

wqsoz72f2#

您需要更改订单,否则更改的捆绑包将无法传递:

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

呼叫 super 类的方法并不总是最顶层的方法。

trnvg8h3

trnvg8h33#

把这条线放好

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

之后 tv_output 已初始化,否则将获得nullpointerexception。
更新:
要在旋转期间将文本保存在textview中,请添加 android:freezesText="true" 对于xml(textview中的(s/g)etfreezestext是什么?),不需要使用onsaveinstancestate。

相关问题