使用Picasso将多个图像从url加载到android应用程序

时间:2018-04-25 22:53:57

标签: android picasso android-webservice

我已经实现了一个Android应用程序,旨在通过Web服务接收图像的可视化。我正在解析从Web服务收到的JSON数据,以便从我将加载图像的位置接收URL。 然后我想使用Picasso将这些图像加载到imageview,但我总是收到以下错误:

  

致命的例外:主要                                                                                处理:com.example.user.sugardraft,PID:21592                                                                                java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.user.sugardraft / com.example.user.sugardraft.GalleryActivity}:java.lang.IllegalArgumentException:Target不能为null。

活动文件是:

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

}

.xml文件是:

<?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>
你能帮帮我吗?有没有其他方法可以加载从图库中的Web服务收到的所有图像?

1 个答案:

答案 0 :(得分:0)

imageView是指您传递给into的{​​{1}}。您的imageView ID与onCreate绑定到R.id.imageview不对应,但您提供的XML文件中不存在此类ID。取而代之的是onCreate put R.id.imageView_gallery