在两种情况下,只有一种情况会给我这个唯一的错误。我基本上都是按照android教程中的每一个字符来操作的,而且出于某种原因,它只在特定的区域工作。
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, getSharedNutritionText(mMultiSelector.getSelectedPositions()));
shareIntent.setType("text/plain");
startActivity(shareIntent);
出现以下错误:
12-13 02:10:07.838 8261-8261/com.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.skylan.homeworkpro, PID: 8261
java.lang.RuntimeException: Parcel: unable to marshal value com.example.NutritionInfo@2d54b4d1
at android.os.Parcel.writeValue(Parcel.java:1343)
at android.os.Parcel.writeList(Parcel.java:717)
at android.os.Parcel.writeValue(Parcel.java:1290)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:644)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
at android.os.Bundle.writeToParcel(Bundle.java:1034)
at android.os.Parcel.writeBundle(Parcel.java:669)
at android.app.FragmentState.writeToParcel(Fragment.java:138)
at android.os.Parcel.writeTypedArray(Parcel.java:1197)
at android.app.FragmentManagerState.writeToParcel(FragmentManager.java:376)
at android.os.Parcel.writeParcelable(Parcel.java:1363)
at android.os.Parcel.writeValue(Parcel.java:1268)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:644)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
at android.os.Bundle.writeToParcel(Bundle.java:1034)
at android.os.Parcel.writeBundle(Parcel.java:669)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2925)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3299)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
而在我的另一个(几乎)相同的片段中,我有以下代码:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, getSharedLabelText(mMultiSelector.getSelectedPositions()));
shareIntent.setType("text/plain");
startActivity(shareIntent);
这是我的营养信息课
public class NutritionInfo extends SugarRecord<NutritionInfo> {
public String nutritionName;
public String nutritionHeaderTitle;
public int nutritionCalories;
public boolean nutritionArchived;
public boolean nutritionChecked;
public NutritionInfo () {}
public NutritionInfo (String name, String headerTitle, int cal, boolean isArchived, boolean isChecked) {
nutritionName = name;
nutritionHeaderTitle = headerTitle;
nutritionCalories = cal;
nutritionArchived = isArchived;
nutritionChecked = isChecked;
}
}
这是我的营养强化剂:
public class NutritionDisplayFragment extends BaseFragment implements ActionMode.Callback {
private String getSharedNutritionText(List<Integer> positions) {
String shareText;
if (positions.equals(null)) {
shareText = "";
} else {
shareText = "Food and carbs:\n";
for (int go = 0; go < positions.size(); go++) {
shareText = shareText +
nutritionList.get(go).nutritionTitle + " - \t" +
nutritionList.get(go).nutritionCarbs + "\n";
}
}
return shareText;
} ( . . . )
}
对比我的标签:
public class LabelManagerFragment extends BaseFragment implements ActionMode.Callback {
private String getSharedLabelText(List<Integer> positions) {
String shareText = "";
if (positions.equals(null)) {
shareText = "";
} else {
for (int go = 0; go < positions.size(); go++) {
shareText = shareText +
labelList.get(go).labelName + "\t" +
labelList.get(go).labelYear + "\n";
}
}
return shareText;
} ( . . . )
}
这不会产生运行时错误。两个get()方法都返回一个简单的字符串。为什么第一个错误会抱怨“com.package.nutritioninfo@84055e1”,它甚至不是要共享的数据类型,因为它是不可封送处理的,而另一个则毫无障碍地共享字符串?
为了进一步说明:我在where the method is(通常返回一个简单的
"hello world"
的方法)中尝试了硬编码String
,但遇到了同样的问题。 最佳答案
实现Parcelable接口,如下所示:
public class NutritionInfo extends SugarRecord<NutritionInfo> implements Parcelable {
public String nutritionName;
public String nutritionHeaderTitle;
public int nutritionCalories;
public boolean nutritionArchived;
public boolean nutritionChecked;
public NutritionInfo() {
}
public NutritionInfo(String name, String headerTitle, int cal, boolean isArchived, boolean isChecked) {
nutritionName = name;
nutritionHeaderTitle = headerTitle;
nutritionCalories = cal;
nutritionArchived = isArchived;
nutritionChecked = isChecked;
}
protected NutritionInfo(Parcel in) {
nutritionName = (String) in.readValue(null);
nutritionHeaderTitle = (String) in.readValue(null);
nutritionCalories = in.readInt();
nutritionArchived = in.readByte() != 0;
nutritionChecked = in.readByte() != 0;
}
public static final Creator<NutritionInfo> CREATOR = new Creator<NutritionInfo>() {
@Override
public NutritionInfo createFromParcel(Parcel in) {
return new NutritionInfo(in);
}
@Override
public NutritionInfo[] newArray(int size) {
return new NutritionInfo[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(nutritionName);
dest.writeValue(nutritionHeaderTitle);
dest.writeInt(nutritionCalories);
dest.writeByte((byte) (nutritionArchived ? 1 : 0));
dest.writeByte((byte) (nutritionChecked ? 1 : 0));
}
}
对你的糖分记录也一样。如果super类实现了parcelable,则无需在nutritioninfo中再次声明,只需实现这些内容。
关于android - java.lang.RuntimeException:包裹:无法编码(marshal)值com.package.NutritionInfo@84055e1,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34264306/