无法将TextView强制转换为java.io.Serializable

时间:2018-09-07 15:54:31

标签: java android-layout serialization

在尝试将其更改为其他布局或更改屏幕方向时,我试图保存文本视图状态:

 @Override
public void onSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState);
  outState.putSerializable("GeneratedNumber", (Serializable) generatedNumber);//I get the error here

}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  generatedNumber = (TextView) savedInstanceState.getSerializable("GeneratedNumber");

}

但是我得到一个错误

  

java.lang.ClassCastException:android.support.v7.widget.AppCompatTextView无法转换为java.io.Serializable

Also GeneratedNumber:是XML文件中的generatedNumber TextView ID。 这是日志:

  

09-07 18:33:41.478 9406-9406 / maskedman.counter E / AndroidRuntime:FATAL EXCEPTION:main       进程:maskedman.counter,PID:9406       java.lang.ClassCastException:android.support.v7.widget.AppCompatTextView无法转换为java.io.Serializable           在maskedman.counter.CounterActivity.onSaveInstanceState(CounterActivity.java:34)           在android.app.Activity.performSaveInstanceState(Activity.java:1474)           在android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1317)           在android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:5393)           在android.app.ActivityThread.performStopActivityInner(ActivityThread.java:4695)           在android.app.ActivityThread.handleStopActivity(ActivityThread.java:4769)           在android.app.ActivityThread.access $ 1400(ActivityThread.java:221)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1823)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:158)           在android.app.ActivityThread.main(ActivityThread.java:7224)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

问题是您尝试将文本视图存储为可序列化的,这是错误的。

检查此:

@Override
public void onSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState);
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putInt("GeneratedNumber", generatedNumber);// generatedNumber is integer
  // etc.
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  generatedNumber = savedInstanceState.getInt("GeneratedNumber");

}

/** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /** 
*
*  layout initailization
*/
    if (savedInstanceState != null) {
       mTextView.setText(String.valueOf(generatedNumber));
    } 

  }

我希望这对您有用。

答案 1 :(得分:0)

尝试仅将textview的值另存为字符串,然后将其再次放入onRestoreInstanceState

在onSaveInstanceState中尝试:

outState.putString("GeneratedNumber", yourTextView.getText().toString);

在onRestoreInstanceState中尝试:

String generatedNumber = savedInstanceState.getString("GeneratedNumber");
yourTextView.setText(generatedNumber);