2

I want to set Image to my Simple Adapter ListView by using Picasso, I tried below code but it shows error as " java.lang.IllegalArgumentException: Target must not be null." in a log, so how can i do this?

//MainActivity.java

StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {

                        try {

                            JSONObject jsonObj = new JSONObject(response);
                            //Log.e(response,"sdsa");
                            // Getting JSON Array node

                            int iserror = jsonObj.getInt("error");
//                            Toast.makeText(MainActivity.this,iserror,Toast.LENGTH_LONG).show();

                            if (iserror == 1) {
                                String error_msg = jsonObj.getString("result");
                                Toast.makeText(Earn.this,error_msg,Toast.LENGTH_LONG).show();
                            }

                            JSONArray result = jsonObj.getJSONArray("result");
    for (int j = 0; j < result.length(); j++) {

                                        JSONObject apps_applist = result.getJSONObject(j);

                                         iddstr = apps_applist.getString("id");
                                         namestr = apps_applist.getString("name");
                                         logostr=apps_applist.getString("logo");


                                        HashMap<String, String> contact = new HashMap<>();

                                        // adding each child node to HashMap key => value
                                        contact.put("id", iddstr);
                                        contact.put("name", namestr);
                                        contact.put("logo", logostr);

                                        Picasso.with(Earn.this)
                                                .load(logostr)
                                                // .placeholder(R.drawable.placeholder)   // optional
                                                //.error(R.drawable.error)      // optional
                                                .resize(400,400)                        // optional
                                                .into((ImageView) findViewById(R.id.Img_Logo));
                                        // adding contact to contact list
                                        contactList.add(contact);

                                    }
                         ListAdapter adapter = new SimpleAdapter(Earn.this, contactList,
                                        R.layout.list_earn, new String[]{ "name","id","logo"},
                                        new int[]{R.id.textView24, R.id.textView23, R.id.Img_Logo});
                                lv.setAdapter(adapter);

//list_earn.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.15">


        <ImageView
            android:layout_width="113dp"
            android:layout_height="107dp"
            app:srcCompat="@mipmap/ic_launcher"
            android:id="@+id/Img_Logo" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView23"
        android:layout_weight="1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id="@+id/textView24"
        android:layout_below="@+id/textView23"
        android:layout_alignLeft="@+id/textView23"
        android:layout_alignStart="@+id/textView23"
        android:layout_marginTop="23dp" />

    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:background="@drawable/button_format"
        android:layout_below="@+id/Img_Logo"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="31dp" />


</RelativeLayout>

</LinearLayout>

//activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">


    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
Rushi K
  • 55
  • 2
  • 9
  • you defined img_view? – Divyesh Patel Mar 08 '17 at 07:15
  • pls post more code. And some error log – Ashish M Mar 08 '17 at 07:16
  • yes ..it is on top – Rushi K Mar 08 '17 at 07:17
  • Check value of logostr.It might be null. – Ragini Mar 08 '17 at 07:50
  • yes it is null...i dont know how it is null? – Rushi K Mar 08 '17 at 08:09
  • You check whether `reference == null`. And of course it is null - the layout containing your `ImageView` will only be inflated inside the `SimpleAdapter`, the layout of your `Activity` (which is the layout that is searched by `findViewById`) does not, at that point, contain such an `ImageView`. And later it will contain multiple such `ImageView`s - one for each list item. In short, I think you can't use Picasso with a `SimpleAdapter` - just write your own. Googling `simpleadapter picasso` takes you [here](http://stackoverflow.com/questions/26118318/how-do-i-pass-picasso-into-listview-adapter). – david.mihola Mar 08 '17 at 12:47
  • Sorry, I misread your previous comment - please ignore my first sentence. – david.mihola Mar 08 '17 at 12:53

0 Answers0