我有两项活动(A和B)。活动A使用startActivityForResult
启动活动B.
从活动B开始,为了回到活动A,我使用了以下代码:
ArrayList<Articles> selectedArticlesList =null;
...
Intent data = new Intent();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("selected-articles", selectedArticlesList);
data.putExtras(bundle);
setResult(RESULT_OK, data);
finish();
类Articles
实现Parcelable
类。
活动A会覆盖方法onActivityResult
:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(data.hasExtra("selected-articles")){
dettServiziListCorrente = new ArrayList<Articles>();
dettServiziListCorrente = data.getExtras().getParcelableArrayList("selected-articles");
Articles a = dettServiziListCorrente.get(0);
Log.i("PRINT DESCRIPTION MEMBER", String.valueOf(a.description));
}
}
我收到以下错误:
09-11 17:29:24.020: E/AndroidRuntime(21499): FATAL EXCEPTION: main
09-11 17:29:24.020: E/AndroidRuntime(21499): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { (has extras) }} to activity {host.activity/host.activity.ActivityA}: java.lang.RuntimeException: bad array lengths
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.app.ActivityThread.access$1100(ActivityThread.java:123)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.os.Handler.dispatchMessage(Handler.java:99)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.os.Looper.loop(Looper.java:137)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-11 17:29:24.020: E/AndroidRuntime(21499): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 17:29:24.020: E/AndroidRuntime(21499): at java.lang.reflect.Method.invoke(Method.java:511)
09-11 17:29:24.020: E/AndroidRuntime(21499): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-11 17:29:24.020: E/AndroidRuntime(21499): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-11 17:29:24.020: E/AndroidRuntime(21499): at dalvik.system.NativeStart.main(Native Method)
09-11 17:29:24.020: E/AndroidRuntime(21499): Caused by: java.lang.RuntimeException: bad array lengths
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.os.Parcel.readStringArray(Parcel.java:838)
09-11 17:29:24.020: E/AndroidRuntime(21499): at host.framework.component.Articles.<init>(Articles.java:30)
09-11 17:29:24.020: E/AndroidRuntime(21499): at host.framework.component.Articles$1.createFromParcel(Articles.java:56)
09-11 17:29:24.020: E/AndroidRuntime(21499): at host.framework.component.Articles$1.createFromParcel(Articles.java:1)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.os.Parcel.readParcelable(Parcel.java:1992)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.os.Parcel.readValue(Parcel.java:1854)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.os.Parcel.readListInternal(Parcel.java:2103)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.os.Parcel.readArrayList(Parcel.java:1544)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.os.Parcel.readValue(Parcel.java:1875)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.os.Parcel.readMapInternal(Parcel.java:2094)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.os.Bundle.unparcel(Bundle.java:223)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.os.Bundle.containsKey(Bundle.java:264)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.content.Intent.hasExtra(Intent.java:3754)
09-11 17:29:24.020: E/AndroidRuntime(21499): at host.activity.ACT_CreazioneOrdine.onActivityResult(ACT_CreazioneOrdine.java:192)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.app.Activity.dispatchActivityResult(Activity.java:4649)
09-11 17:29:24.020: E/AndroidRuntime(21499): at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
09-11 17:29:24.020: E/AndroidRuntime(21499): ... 11 more
我对Articles类的实现是:
public class Articles implements Parcelable{
public int id;
public String description;
public String immagine;
public int quantita;
public Articles (int id,String descriprion, String immagine){
this.id = id;
this.description = descriprion;
this.immagine = immagine;
this.quantita = 0;
}
public Articles (int id,String descriprion, String immagine,int quantita){
this.id = id;
this.description = descriprion;
this.immagine = immagine;
this.quantita = quantita;
}
public Articles (Parcel in){
String[] data = new String[3];
in.readStringArray(data);
this.id = Integer.parseInt(data[0]);
this.description = data[1];
this.immagine = data[2];
this.quantita = Integer.parseInt(data[3]);
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeStringArray(new String[] {
String.valueOf(this.id),
this.description,
this.immagine,
String.valueOf(this.quantita)
});
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Articles createFromParcel(Parcel in) {
return new Articles (in);
}
public Articles [] newArray(int size) {
return new Articles [size];
}
};
}
答案 0 :(得分:4)
问题在于这个功能:
public Articles (Parcel in)
{
String[] data = new String[3];// here you defined array of 3 elements it should be 4
in.readStringArray(data);
this.id = Integer.parseInt(data[0]);
this.description = data[1];
this.immagine = data[2];
this.quantita = Integer.parseInt(data[3]);
}
因为在这一行中你传递了四个元素:
dest.writeStringArray(new String[]
{ String.valueOf(this.id), this.description, this.immagine, String.valueOf(this.quantita) });