2015-08-27 62 views
1

我創建了一個實現可執行程序接口的類。然後我創建了該類的數組列表,並將其意圖傳遞給另一個活動。但是,當活動嘗試獲取parcelable數組列表中運行時異常happpening可執行程序對象不能正常工作的列表

我parcelable類是

package com.myapps.sulu.firstapp; 
import android.os.Parcel; 
import android.os.Parcelable; 
public class Details implements Parcelable { 
int id = 0; 
String name = ""; 
int number = 0; 

public Details(int id, String name, int number) { 
    this.id = id; 
    this.name = name; 
    this.number = number; 
} 

public Details(Parcel in) { 
    this.id = in.readInt(); 
    this.name = in.readString(); 
    this.number = in.readInt(); 
} 

@Override 
public int describeContents() { 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeInt(id); 
    dest.writeInt(number); 
    dest.writeString(name); 
} 

public int getId() { 
    return id; 
} 

public String getName() { 
    return name; 
} 

public int getNumber() { 
    return number; 
} 


public static final Creator<Details> CREATOR = new Creator<Details>() { 
    @Override 
    public Details createFromParcel(Parcel in) { 
     return new Details(in); 
    } 

    @Override 
    public Details[] newArray(int size) { 
     return new Details[size]; 
    } 
}; 
} 

我在這裏創建數組列表

Cursor cursor = writableDatabase.query(TestContracts.TestTable.TABLE_NAME, projection, 
      null, null, null, null, null); 
    cursor.moveToFirst(); 
    ArrayList<Details> allDetailses = new ArrayList<Details>(); 
    do { 
     int id = cursor.getInt(cursor.getColumnIndex(TestContracts.TestTable.ID)); 
     int phone_number = cursor.getInt(cursor.getColumnIndex(TestContracts.TestTable.NUMBER)); 
     String name = cursor.getString(cursor.getColumnIndex(TestContracts.TestTable.NAME)); 
     Details details = new Details(id, name, phone_number); 
     Log.d("MSG", name + " " + id + " " + phone_number); 
     allDetailses.add(details); 
    } while (cursor.moveToNext()); 

    Intent intent = new Intent(DbActivity.this, DetailsActivity.class); 
    intent.putParcelableArrayListExtra("values", allDetailses); 
    startActivity(intent); 

我得到了例外getParcalableArrayList行如下所示

Intent intent = getIntent(); 
    values = intent.getParcelableArrayListExtra("values"); 
    showRows(); 

例外情況i小號

08-27 16:48:39.507 7277-7277/com.myapps.sulu.firstapp E/AndroidRuntime﹕ FATAL EXCEPTION: main 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapps.sulu.firstapp/com.myapps.sulu.firstapp.DetailsActivity}: java.lang.RuntimeException: Parcel [email protected]: Unmarshalling unknown type code 7536737 at offset 124 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309) 
     at android.app.ActivityThread.access$700(ActivityThread.java:157) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:176) 
     at android.app.ActivityThread.main(ActivityThread.java:5317) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
     at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.RuntimeException: Parcel [email protected]: Unmarshalling unknown type code 7536737 at offset 124 
     at android.os.Parcel.readValue(Parcel.java:2032) 
     at android.os.Parcel.readListInternal(Parcel.java:2235) 
     at android.os.Parcel.readArrayList(Parcel.java:1655) 
     at android.os.Parcel.readValue(Parcel.java:1986) 
     at android.os.Parcel.readMapInternal(Parcel.java:2226) 
     at android.os.Bundle.unparcel(Bundle.java:223) 
     at android.os.Bundle.getParcelableArrayList(Bundle.java:1217) 
     at android.content.Intent.getParcelableArrayListExtra(Intent.java:4837) 
     at com.myapps.sulu.firstapp.DetailsActivity.onCreate(DetailsActivity.java:25)    
     at android.app.Activity.performCreate(Activity.java:5326) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)  
     at android.app.ActivityThread.access$700(ActivityThread.java:157) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:176) 
     at android.app.ActivityThread.main(ActivityThread.java:5317) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
     at dalvik.system.NativeStart.main(Native Method) 
+0

是否要將整個遊標數據添加到Intent的額外?它真的是你想要做的嗎? – pskink

+0

是的,要在另一個活動 –

+0

中顯示錶格,然後傳遞信息「如何」獲取光標,不傳遞所有數據,如果您有成千上萬的行,那該怎麼辦? – pskink

回答

2

的問題是,您填寫包裹順序如下:

  • id
  • number
  • name

而當你試圖重建自己使用的包裹物此訂單:

  • id
  • name
  • number

重要的是,當你把你的價值觀到您做以相同的順序,當你從包裹閱讀他們的包裹。因此,您必須在創建者調用的構造函數中或在writeToParcel方法中切換namenumber

0

您需要實現readFromParcel方法在Details類。