我正在尝试将我的玩家数组保存为JSONArray
,将其转换为字符串,将其放入SharedPreferences
然后将其转换回来。如果JSONArray
我尝试访问数组中的任何 Player 实例,我在使用当前尝试重新加载我的应用程序时会返回运行时异常/ClassCast 异常。
我哪里错了?
JSONArray savedPlayers;
SharedPreferences prefs;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.indie);
savedPlayers = new JSONArray();
prefs = getSharedPreferences("appData", 0);
String jsonString = prefs.getString("playerString", null);
//Restores playerArray if any players have been saved in the past
if(jsonString != null)
{
try
{
savedPlayers = new JSONArray(JSONString);
MyListView.players.clear();
MyListView.ids.clear();
for(int i = 0; i < savedPlayers.length(); i++)
{
MyListView.players.add(((Player) savedPlayers.get(i)).getName()); //Error
MyListView.ids.add(((Player) savedPlayers.get(i)).getSaveId());
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}
//If there are savedPlayers converts the JSONArray to a string and saved it within SharedPreferences
@Override
protected void onPause()
{
if(savedPlayers != null)
{
SharedPreferences.Editor editor = getSharedPreferences("appData", 0).edit();
String jsonString = savedPlayers.toString();
editor.putString("playerString", jsonString).commit();
}
super.onPause();
}
JSON字符串:
["en.deco.android.livehud.Player@44eba878","en.deco.android.livehud.Player@44ebec68"]
日志猫:
04-16 22:31:13.765: ERROR/AndroidRuntime(7496): Uncaught handler: thread main exiting due to uncaught exception
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): java.lang.RuntimeException: Unable to start activity ComponentInfo{en.deco.android.livehud/en.deco.android.livehud.GUI}: java.lang.ClassCastException: java.lang.String
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): at android.os.Looper.loop(Looper.java:123)
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): at java.lang.reflect.Method.invoke(Method.java:521)
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): at dalvik.system.NativeStart.main(Native Method)
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): Caused by: java.lang.ClassCastException: java.lang.String
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): at en.deco.android.livehud.GUI.onCreate(GUI.java:102)
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-16 22:31:13.785: ERROR/AndroidRuntime(7496): ... 11 more
04-16 22:32:57.775: ERROR/AndroidRuntime(7537): Uncaught handler: thread main exiting due to uncaught exception
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): java.lang.RuntimeException: Unable to start activity ComponentInfo{en.deco.android.livehud/en.deco.android.livehud.GUI}: java.lang.ClassCastException: java.lang.String
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): at android.os.Looper.loop(Looper.java:123)
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): at java.lang.reflect.Method.invoke(Method.java:521)
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): at dalvik.system.NativeStart.main(Native Method)
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): Caused by: java.lang.ClassCastException: java.lang.String
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): at en.deco.android.livehud.GUI.onCreate(GUI.java:102)
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-16 22:32:57.795: ERROR/AndroidRuntime(7537): ... 11 more