0

I have implemented an android application aiming at the visualization of the images received through web services. I am parsing the JSON data received from the web service in order to receive the url from where I will load the image. Then I want to load those images to an imageview using Picasso, though I always receive the error below:

FATAL EXCEPTION: main Process: com.example.user.sugardraft, PID: 21592 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.sugardraft/com.example.user.sugardraft.GalleryActivity}: java.lang.IllegalArgumentException: Target must not be null.

The activity file is:

public class Activity extends AppCompatActivity {
String url = "https://..../wp-json/wp/v2/media";
List<Object> list;
Gson gson;
ListView postList;
GridView gridView;
GridAdapter myGridAdapter;

Map<String,Object> mapPost;
Map<String,Object> mapTitle;
int postID;
String postTitle[];
ImageView imageView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news);

    imageView = (ImageView)findViewById(R.id.imageView);

    StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {
            gson = new Gson();
            list = (List) gson.fromJson(s, List.class);
            postTitle = new String[list.size()];
            for (int i = 0; i < list.size(); ++i) {
                mapPost = (Map<String, Object>) list.get(i);
                mapTitle = (Map<String, Object>) mapPost.get("guid");
                postTitle[i] = (String) mapTitle.get("rendered");
                Log.d("Value", "Value: " + postTitle[i].toString());
                Picasso.get().load(postTitle[i]).into(imageView);
            }
            progressDialog.dismiss();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Toast.makeText(Activity.this, "Some error occurred", Toast.LENGTH_LONG).show();
        }
    });

    RequestQueue rQueue = Volley.newRequestQueue(Activity.this);
    rQueue.add(request);

}

And the .xml file is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
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"
tools:context="com.example.user.sugardraft.MainActivity"
android:orientation="vertical"
>
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/fotologo2"
    />
 <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"> 
 </ImageView>
 </LinearLayout>

Could you please help me? Is there any other way in order to load all the images received form the web service in a gallery?

MTs
  • 199
  • 2
  • 19

1 Answers1

0

The Target refers to the imageView that you pass to into. Your imageView ids do not correspond, in your onCreate you bind it to R.id.imageview, but such an id doesn't exist in the XML file that you provided. Instead in onCreate put R.id.imageView_gallery.

Suleyman
  • 2,765
  • 2
  • 18
  • 31
  • Thank you for the answer @LieForBananas. I have updated the code above and so the code on my application. Though I am having the same error again. Any ideas please? – MTs Apr 25 '18 at 23:08
  • So it's still the problem with the imageView, try to change the id in the XML to something unique like `android:id="@+id/imageViewNew"` and change it in your activity as well when you do `findViewById`. – Suleyman Apr 25 '18 at 23:13
  • I have made the change and now the error is Process: com.example.user.test, PID: 25181 java.lang.IllegalArgumentException: Target must not be null. at com.squareup.picasso.RequestCreator.into(RequestCreator.java:682) – MTs Apr 26 '18 at 08:50
  • @MariaTsourma so you changed in XML to `android:id="@+id/imageViewNew" ` and in `onCreate` to `imageView = (ImageView)findViewById(R.id.imageViewNew);` ? – Suleyman Apr 26 '18 at 09:45