简体   繁体   中英

Cannot cast TextView to java.io.Serializable

I am trying to save my text view state when changing the layout it is in to another layout or changing the screen orientation:

 @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");

}

but I get an error

java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to java.io.Serializable

Also GeneratedNumber: is the generatedNumber TextView id in the XML file. This is the log:

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

Can anybody help me ?

The issue is that you try to store the text view as serializable which is wrong.

Check this :

@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));
    } 

  }

I hope this works for you.

Try to save just the value of the textview as string and the put it again in the onRestoreInstanceState

In the onSaveInstanceState try:

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

In the onRestoreInstanceState try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM