java.util.ArrayList无法强制转换为android.os.Parcelable。 key expected Parcelable但value是java.util.ArrayList

时间:2016-09-13 06:37:53

标签: android

以下错误的原因是什么

Key Object_list expected Parcelable but value was a java.util.ArrayList.  The default value <null> was returned.

这是完整代码

public class SingleObjectActivity extends Activity
{
    public static final String OBJECT_LIST = "Object_list";
    private ArrayList<Listitem> Objects;
    public ImageView imgview;
    private Listitem listObjects;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.singleobject);
        Bundle extras = getIntent().getExtras();

        imgview = (ImageView) findViewById(R.id.funnyimage);

        if (extras.containsKey(OBJECT_LIST)) {
            this.listObjects = extras.getParcelable(OBJECT_LIST);
        } else {
            this.listObjects = null;
        }

        if (listObjects != null) {
            Picasso.
                    with(getApplicationContext()).
                    load(listObjects.getUrl())
                            //load()
                    .placeholder(R.drawable.logo)
                    .fit()
                    .noFade()
                    .into(imgview);

        }
    }
}

发送意图

        ArrayList<Listitem> personArrayList = new ArrayList<>();
        personArrayList.add(new Listitem(item.getOrder(), item.getId(), item.getUrl(), item.getUserName(), item.getLikes()));

        Intent intent = new Intent(mcontext,SingleObjectActivity.class);
        intent.putExtra("Object_list", personArrayList);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mcontext.startActivity(intent);

编辑4

        ArrayList<Listitem> items = getIntent().getParcelableArrayListExtra("Object_list");
        Bundle extras = getIntent().getExtras();

        imgview = (ImageView) findViewById(R.id.funnyimage);

     /*   if (extras != null) {
            Objects = extras.getParcelableArrayList(OBJECT_LIST);
        } else {
            Objects = new ArrayList<Listitem>();
        }*/

        if (extras.containsKey(OBJECT_LIST)) {
//            this.listObjects = extras.getParcelable(OBJECT_LIST);
         //   this.listObjects = (Listitem) extras.getParcelable(OBJECT_LIST);
            this.Objects=  extras.getParcelableArrayList("Object_list");


        } else {
            this.Objects = null;
        }

        if (this.listObjects != null) {
            Picasso.
                    with(getApplicationContext()).
                    load(this.listObjects.getUrt())
                            //load()
                    .placeholder(R.drawable.logo)
                    .fit()
                    .noFade()
                    .into(imgview);

        }
    }

5 个答案:

答案 0 :(得分:5)

发送ArrayList时:

Intent intent = new Intent(context ,TargetActivity.class);
        intent.putParcelableArrayListExtra("Object_list", personArrayList);
        context.startActivity(intent);

收到ArrayList时:

ArrayList<ListItem> items = getIntent().getParcelableArrayListExtra("Object_list");

请注意,这与getIntent().getExtras().getParcelableArrayList()不同。目前,我认为你写错了

Bundle extras = geIntent().getExtras();
extras.getParcelableArrayList("Object_list");

此外,ListItem尽管已实施Parcelable,但并未复制其所有值。这可能不会导致您的问题,但值得记住。

答案 1 :(得分:1)

我认为你应该这样做:

 intent.putParcelableArrayListExtra("Object_list", (ArrayList<? extends Parcelable>) personArrayList);

进入预期的活动,如:

 this.Objects= extras.getParcelableArrayList("Object_list");

确保Listitem类正在实现Parcelable。

答案 2 :(得分:0)

你可以试试这个:

ArrayList

获取extras
this.listObjects = (Listitem) extras.getParcelable(OBJECT_LIST);

答案 3 :(得分:0)

1.制作模型类,即ListItem Parcelable

2。 第二步,

Header

3。 ArrayList list = bundle.getParcelableArrayList(&#34; list&#34;);

答案 4 :(得分:0)

在发送 Intent 代码 intent.putExtra("Object_list", personArrayList); 时替换为 intent.putParcelableArrayListExtra("Object_list", personArrayList); 将解决问题