gpt4 book ai didi

java - 方向更改时未找到 Savedins 类

转载 作者:行者123 更新时间:2023-12-01 22:17:43 25 4
gpt4 key购买 nike

当屏幕方向发生变化时,我尝试使用 savingInstance 和 Bundle 来补偿我的列表项,但我不断收到多个 java.lang.ClassNotFoundException 错误。当我单步执行代码时,它似乎将该项目添加到 bundle 中,但是当我尝试在 Debug模式下查看它时,它显示 java.lang.ClassNotFoundException 错误。我想指出的是,“ts”的可序列化值显示了相同的错误,但是当我旋转屏幕时,原始时间保持不变。

我真的不确定发生了什么,非常感谢任何帮助。

主要 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]);
}
}
}

}
}

已保存实例//

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

堆栈跟踪//

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)

最佳答案

关于 onSaveInstanceState您存储 ArrayList<String>对于 listItems ,但是检索 listItems 时你使用getStringArray("listItems")它返回 String[] .

检索listItems您应该使用以下内容:

ArrayList<String> values = savedInstanceState.getStringArrayList("listItems");

关于java - 方向更改时未找到 Savedins 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30657952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com