-2

what is the reason of the below error

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

this is full code

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

        }
    }
}

sending intent

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

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

        }
    }
Moudiz
  • 7,211
  • 22
  • 78
  • 156

5 Answers5

6

When sending your ArrayList:

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

When receiving your ArrayList:

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

Note that this is not the same as getIntent().getExtras().getParcelableArrayList(). Currently, I believe you are incorrectly writing

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

Also, ListItem, despite implementing Parcelable, does not copy all of its values. This probably isn't causing your issue but is worth keeping in mind.

PPartisan
  • 8,173
  • 4
  • 29
  • 48
  • but how to get the value of url ? please check my edit when applying your code – Moudiz Sep 13 '16 at 07:40
  • @Moudiz As I believe you've already been told, you access the relevant index in your `ArrayList` with the `get()` method to return a `ListItem` object, to which you can call `getUrl()`. It's impossible to tell which index you want from your code however. If you just wanted to test it out, try accessing index `0` - `listItems.get(0).getUrl()`. – PPartisan Sep 13 '16 at 07:53
  • hi man i am still having problem in my code , can you assist me? – Moudiz Sep 13 '16 at 11:28
  • perfect explanation – Hiren Jul 12 '18 at 10:24
1

I think you should do :

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

and get inside intended activity like :

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

Make sure that Listitem class is implementing Parcelable.

Sar
  • 550
  • 4
  • 18
  • there is no `getParcelableArrayListExtra` – Moudiz Sep 13 '16 at 07:07
  • @Moudiz There is, it's [here](https://developer.android.com/reference/android/content/Intent.html#getParcelableArrayListExtra(java.lang.String)) in the documentation. Note you want to use `getIntent().getParcelbleArrayListExtra()`, and not `getIntent().getExtras().getParcelableArrayListExtra()` – PPartisan Sep 13 '16 at 07:15
  • You can use extras.getParcelableArrayList() method – Sar Sep 13 '16 at 07:15
  • @Sar still i get error using it `this.listObjects= extras.getParcelableArrayList("Object_list");` required lisitem found arraylist .. is it maybe problem in listobjects because its declared listitem ? – Moudiz Sep 13 '16 at 07:19
  • You should do this.Objects not this.listObjects – Sar Sep 13 '16 at 07:20
  • @sar okay... then in picasso to get the image `load(listObjects.getUrl())` how to get listobject ? – Moudiz Sep 13 '16 at 07:26
  • Use Objects.get(position).getUrt() for getting particular item's url from list – Sar Sep 13 '16 at 07:29
  • and how do i get position? – Moudiz Sep 13 '16 at 07:32
  • Do you want only a single ListItem in SingleObjectActivity not all the list? – Sar Sep 13 '16 at 07:35
  • in my scenario i want only the url passed from the intent – Moudiz Sep 13 '16 at 07:36
  • Then there is no need to pass the whole list of items. You should pass only the url as string. – Sar Sep 13 '16 at 07:39
  • @Sar later ill be sending another things. why I am not able to get the arraylist ? – Moudiz Sep 13 '16 at 07:41
  • You should pass only intent.putExtra("key", item.getUrl); – Sar Sep 13 '16 at 07:44
  • can you show me in an exmaple because i am lost now – Moudiz Sep 13 '16 at 07:53
0

You may try this :

To get ArrayList from extras

this.listObjects = (Listitem) extras.getParcelable(OBJECT_LIST);
Dipali Shah
  • 3,742
  • 32
  • 47
0

1.Make you model class i.e. ListItem Parcelable

2. in second step,

Bundle bundle = new Bundle(); bundle.putParcelableArrayList("list",list); intent.putExtras(bundle);

3. ArrayList list = bundle.getParcelableArrayList("list");

rushi
  • 277
  • 2
  • 8
0

In sending intent code intent.putExtra("Object_list", personArrayList); replace with this intent.putParcelableArrayListExtra("Object_list", personArrayList); will solve problem

Mohd Qasim
  • 896
  • 9
  • 20