0

我想在 onSaveInstanceState 上保存 Parcelable Restaurant 对象的列表。我创建了一个扩展 Parcelable 的 Restaurant 类和一个 RestaurantList 对象,如下所示。在我的主要活动中,我使用 GSON 直接从 json 互联网数据填充我的 RestauranList:

restaurantList = gson.fromJson(r, RestaurantList.class);

现在,当我想使用保存餐厅对象列表时

protected void onSaveInstanceState(Bundle outState) {           

    if (restaurantList != null ) {      
        outState.putParcelableArrayList("restoList", (ArrayList<Restaurant>)restaurantList.getRestaurants());
    }       

    super.onSaveInstanceState(outState);
}

我收到一个错误(如下所示),我猜是因为未写入 Parcelable 数据。我该怎么做?我是否必须在我的主要活动中创建一个 Parcel 对象数组并通过为每个 Restaurant 对象调用 writeToParcel 来填充它?

感谢您的任何建议

朱利安

++++++

LogCat(它在 outState.putParcelableArrayList("restoList", (ArrayList)restaurantList.getRestaurants()); 中崩溃)

07-05 19:34:13.846: WARN/ActivityManager(563): Activity pause timeout for HistoryRecord{436950f8 {org.digitalfarm.atable/org.digitalfarm.atable.Atable}}
07-05 19:34:28.336: DEBUG/AndroidRuntime(808): Shutting down VM
07-05 19:34:28.336: WARN/dalvikvm(808): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
07-05 19:34:28.336: ERROR/AndroidRuntime(808): Uncaught handler: thread main exiting due to uncaught exception
07-05 19:34:28.637: DEBUG/dalvikvm(808): GC freed 3337 objects / 259288 bytes in 94ms
07-05 19:34:28.647: ERROR/AndroidRuntime(808): java.lang.RuntimeException: Unable to pause activity {org.digitalfarm.atable/org.digitalfarm.atable.Atable}: java.lang.ClassCastException: java.util.LinkedList
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2840)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2797)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2780)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.access$2000(ActivityThread.java:112)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1699)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.os.Looper.loop(Looper.java:123)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.main(ActivityThread.java:3948)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at java.lang.reflect.Method.invokeNative(Native Method)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at java.lang.reflect.Method.invoke(Method.java:521)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at dalvik.system.NativeStart.main(Native Method)
07-05 19:34:28.647: ERROR/AndroidRuntime(808): Caused by: java.lang.ClassCastException: java.util.LinkedList
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at org.digitalfarm.atable.Atable.onSaveInstanceState(Atable.java:254)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.Activity.performSaveInstanceState(Activity.java:1008)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1256)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2822)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     ... 12 more

RestaurantList 类

public class RestaurantList {

    private List<Restaurant> restaurants = new ArrayList<Restaurant>();

    public int getSize() {
        return restaurants.size();          
    }

    public List<Restaurant> getRestaurants() {
        return this.restaurants;
    }       

    public Restaurant getRestaurant(int i) {
        return this.restaurants.get(i);
    }

    public void setRestaurants(List<Restaurant> restaurants) {
        this.restaurants = restaurants;
    }

    public List<String> getRestaurantNames() {

        List<String> restaurantNames = new ArrayList<String>();

        for (int i=0; i<this.restaurants.size(); i++) {
            restaurantNames.add(this.restaurants.get(i).getName());             
        }

        return restaurantNames;
    }

}

餐厅类

public class Restaurant implements Parcelable{

    private String name;
    /* more stuff */

    public Restaurant() {}          

    public String getName() {
        return this.name;   
    }

    public void setName(String name) {
        this.name = name;
    }       

    public void writeToParcel(Parcel out, int flags) {  

        out.writeStringArray(new String[] {this.name, /* more stuff*/});            

    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {

            public Restaurant createFromParcel(Parcel in) { return new Restaurant(in); }

            public Restaurant[] newArray(int size) { return new Restaurant[size]; }

    };


    private Restaurant(Parcel in) {

        String[] stringData = new String[6];                

        in.readStringArray(stringData);

        this.name = stringData[0];  
                /* more stuff */     

    }       

    public int describeContents(){ 
        return 0;
    }


}
4

1 回答 1

1

您收到 ClassCastException 因为您的列表是 LinkedList 并且您将其转换为抛出异常的 ArrayList 。您的餐厅课程似乎没有任何问题。

您应该将 ArrayList 用于您的 Parcelable List,我不明白为什么您需要一个单独的 RestaurantList 类。

于 2010-07-05T17:51:25.937 回答