0

I'm trying to compensate for my list items when the screen orientation changes by using the savedInstance and Bundle but I keep getting multiple java.lang.ClassNotFoundException errors. When I step through the code it appears to add the item into the bundle, but when I try to view it while in debug mode it shows with the java.lang.ClassNotFoundException error. I'd like to point out that the serializable value of "ts" shows the same error, but when I rotate my screen the original time is maintained.

I'm really not sure what is going on, any help is appreciated.

Main activity//

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

    getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        if (savedInstanceState != null)
        {
            if ((savedInstanceState != null) && (savedInstanceState.getSerializable("ts") != null))
            {
                startTime = (Calendar) savedInstanceState.getSerializable("starttime");
                String samp = (String) savedInstanceState.getSerializable("ts");
                refreshTS = (TextView) findViewById(R.id.timeStamp);
                refreshTS.setText(samp);

                String[] values = savedInstanceState.getStringArray("listItems");
                if (values != null) {
                    for (int i = 0; i< values.length; i++) {
                        mMainListAdapter.add(values[i]);
                    }
                }
            }

        }
}

savedInstance//

@Override
protected void onSaveInstanceState(Bundle state) {

    super.onSaveInstanceState(state);
    state.putSerializable("starttime", startTime);
    state.putSerializable("ts",refreshTS.getText().toString());

    ArrayList<String> values = new ArrayList<String>() ;
    for (int i =0; i< mMainListAdapter.getCount(); i++)
    {
        String obj = mMainListAdapter.getItem(i);
        values.add(obj);
    }

    state.putStringArrayList("listItems", values);
}

Stack trace//

06-05 07:27:08.394  13901-13901/commyapp.myapp W/Bundle﹕ Attempt to cast generated internal exception:
    java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String[]
            at android.os.Bundle.getStringArray(Bundle.java:1565)
            at commyapp.myapp.Summary.onCreate(Summary.java:90)
            at android.app.Activity.performCreate(Activity.java:5600)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2504)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2599)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4218)
            at android.app.ActivityThread.access$1000(ActivityThread.java:174)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5748)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
snapplex
  • 851
  • 3
  • 13
  • 27
  • Do you have a stack trace of the ClassNotFoundException? – lordoku Jun 05 '15 at 03:42
  • Unrelated, and unlikely to be related to crash, you're checking for savedInstanceState to be null twice, can remove that. – Zadrox Jun 05 '15 at 03:50
  • Did you implement Serializable correctly? – almightyGOSU Jun 05 '15 at 06:03
  • @lordoku, I've included the stack trace. It is hinting towards a type mismatch: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String[] Although, I thought I implemented it correctly. :\ – snapplex Jun 05 '15 at 11:37
  • @Gosu, I was following a tutorial, so I assume it is correct since the time is being passed back/forth. – snapplex Jun 05 '15 at 11:38

1 Answers1

3

On onSaveInstanceState you store an ArrayList<String> for listItems, but when retrieving listItems you use getStringArray("listItems") which returns a String[].

To retrieve listItems you should use the following:

ArrayList<String> values = savedInstanceState.getStringArrayList("listItems");
Mokus
  • 3,339
  • 1
  • 24
  • 30