java.lang.RuntimeException:Parcel:无法编组值com.package.NutritionInfo@84055e1

时间:2015-12-14 10:08:56

标签: android android-intent send parcel

在两种情况下,只有其中一种情况给我这个独特的错误。我非常关注角色的Android Tutorial角色,由于某种原因它只适用于某些领域。

 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);

这是我的NutritionInfo类

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;
} ( . . . )
}

vs我的Label getter:

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 ”,这甚至不是被共享的数据类型,因为是非编组的,而另一个错误地共享字符串拴住?

为了进一步明确:我尝试将"hello world"硬编码到方法的位置(通常返回简单String的方法),并且遇到同样的问题。< / p>

1 个答案:

答案 0 :(得分:1)

实现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));
  }
}

为您的糖记录做同样的事情。如果超类实现了Parcelable,你不需要在NutritionInfo中再次声明,只实现这些东西。