-1

我有一个Weekly Result实现 Parcel 接口的类。当我将包裹对象传递给另一个活动时,我在检索包裹对象时遇到问题。

这是我的weeklyResult课:

public class WeeklyTopicResult implements Parcelable {

public String ID;
public MultiLingual Title = new MultiLingual();
public MultiLingual Text = new MultiLingual();
public String ImageUrl;
public String PublishOn;
public Bitmap ImageSource;

public WeeklyTopicResult(Parcel in) {
    // Reads the obj value
    ReadFromParcel(in);

}

public WeeklyTopicResult() {
    // TODO Auto-generated constructor stub
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
    Log.v("WeekData", "WriteToParcel..." + flags);
    dest.writeString(ID);
    dest.writeString(ImageUrl);
    dest.writeString(PublishOn);
    dest.writeParcelable(Title, flags);
    dest.writeParcelable(Text, flags);
    dest.writeParcelable(ImageSource, flags);

}

public void ReadFromParcel(Parcel obj) {
    Log.v("WeeklyClaas",
            "ParcelData(Parcel source): time to put back parcel data");
    ID = obj.readString();
    Title = (MultiLingual) obj.readParcelable(MultiLingual.class
            .getClassLoader());
    Text = (MultiLingual) obj.readParcelable(MultiLingual.class
            .getClassLoader());
    ImageUrl = obj.readString();
    PublishOn = obj.readString();
    ImageSource = (Bitmap) obj
            .readParcelable(Bitmap.class.getClassLoader());

}

public class WeeklyTopicResultParcelCreater implements
        Parcelable.Creator<WeeklyTopicResult> {

    @Override
    public WeeklyTopicResult createFromParcel(Parcel source) {
        // TODO Auto-generated method stub
        return new WeeklyTopicResult(source);
    }

    @Override
    public WeeklyTopicResult[] newArray(int size) {
        // TODO Auto-generated method stub
        return new WeeklyTopicResult[size];
    }
}
}

这是我的Multilinqual课:

public class MultiLingual implements Parcelable {

public String ArbicValue;

public String EnglishValue;

public MultiLingual(Parcel in) {
    ReadFromParcel(in);
}

public MultiLingual() {
    // TODO Auto-generated constructor stub
}

@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.writeString(ArbicValue);
    dest.writeString(EnglishValue);

}

private void ReadFromParcel(Parcel Obj) {

    ArbicValue = Obj.readString();
    EnglishValue = Obj.readString();
}

public class MyMultiLingualParcelCreator implements
        Parcelable.Creator<MultiLingual> {

    @Override
    public MultiLingual createFromParcel(Parcel source) {
        // TODO Auto-generated method stub
        return new MultiLingual(source);
    }

    @Override
    public MultiLingual[] newArray(int size) {
        // TODO Auto-generated method stub
        return new MultiLingual[size];
    }

}
}

这是我发送意图的对象的地方:

public void GoToWeeklyTopic(View v) {
    Intent intent = new Intent();
    intent.setClass(HomeActivity.this, Topic_of_the_week.class);

    Bundle b = new Bundle();
    WeeklyTopicResult w = weeks;

    // Puts the weeklyresult parcel class into bundle
    b.putParcelable("WeeklyContent", w);

    intent.putExtras(b);
    startActivity(intent);
}

这是我试图接收包裹对象的活动类:

ImageView weekImg;
TextView title;
TextView ContentText;
WeeklyTopicResult week= new WeeklyTopicResult();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_topic_of_the_week);

    Bundle extras = getIntent().getExtras();
            //Here I am receiving an error
    week =extras.getParcelable("WeeklyContent");


    weekImg = (ImageView) findViewById(R.id.imageView1);
    title = (TextView) findViewById(R.id.txt_MainTitle);
    ContentText = (TextView) findViewById(R.id.txt_page_content_topicweek);

    title.setText(week.Title.EnglishValue);
    ContentText.setText(week.Text.EnglishValue);
    weekImg.setImageBitmap(week.ImageSource);
    // String FullPath = RetrieveData_Preference("WeeklyTopicUrl") + Url;
    // getImage(FullPath);

}

我什至尝试将对象投回,但仍然出现错误。Logcat 错误向我显示了此错误,但我已经为每周结果实现了一个创建者类

LogCat 错误:

致命异常:主要
java.lang.RuntimeException:无法启动活动

ComponentInfo{com.example.laysapp/com.example.laysapp.AdminPage.Topic_of_the_week}: android.os.BadParcelableException:Parcelable 协议需要在类 com.example.laysapp.ParsingLogic.WeeklyTopicResult E/AndroidRuntime
上名为 CREATOR 的Parcelable.Creator 对象(
1231):
在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
在 android.app.ActivityThread.access$600(ActivityThread.java:141)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
在 android.os.Handler.dispatchMessage(Handler.java:99)
在 android.os.Looper.loop(Looper.java:137) 在 android.app.ActivityThread.main(ActivityThread.java:5039) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect .Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) ) 在 dalvik.system.NativeStart.main(Native Method) 引起:android.os.BadParcelableException:Parcelable 协议需要一个 Parcelable.Creator 对象,在 android.os.Parcel 的类 com.example.laysapp.ParsingLogic.WeeklyTopicResult 上称为 CREATOR。 readParcelable(Parcel.java:2086) 在 android.os.Parcel.readValue(Parcel.java:1965) 在 android.os.Parcel.readMapInternal(Parcel.java:2226)在android.os.Bundle.unparcel(Bundle.java:223)在android.os.Bundle.getParcelable(Bundle.java:1165)在com.example.laysapp.AdminPage.Topic_of_the_week.onCreate(Topic_of_the_week.java:40 ) 在 android.app.Activity.performCreate(Activity.java:5104) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) ... 11更多的callActivityOnCreate(Instrumentation.java:1080) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) ... 11 更多callActivityOnCreate(Instrumentation.java:1080) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) ... 11 更多

我在这里做错了什么?

4

2 回答 2

4

从文档中:

实现 Parcelable 接口的类还必须有一个名为 CREATOR 的静态字段,它是一个实现 Parcelable.Creator 接口的对象。

因此,将此字段添加到您的MultiLingual课程中:

public static final MyMultiLingualParcelCreator CREATOR = new MyMultiLingualParcelCreator();
于 2012-12-26T18:24:31.467 回答
0

你总是必须按照你写的顺序阅读:

@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
    Log.v("WeekData", "WriteToParcel..." + flags);
    dest.writeString(ID);
    dest.writeString(ImageUrl);
    dest.writeString(PublishOn);
    dest.writeParcelable(Title, flags);
    dest.writeParcelable(Text, flags);
    dest.writeParcelable(ImageSource, flags);

}

public void ReadFromParcel(Parcel obj) {
    Log.v("WeeklyClaas",
            "ParcelData(Parcel source): time to put back parcel data");
    ID = obj.readString();
    //these were lower
    ImageUrl = obj.readString();
    PublishOn = obj.readString();
    Title = (MultiLingual) obj.readParcelable(MultiLingual.class
            .getClassLoader());
    Text = (MultiLingual) obj.readParcelable(MultiLingual.class
            .getClassLoader());
    ImageSource = (Bitmap) obj
            .readParcelable(Bitmap.class.getClassLoader());
}
于 2012-12-26T18:25:17.007 回答