如何使用Volley Library的ImageLoader?

时间:2015-03-03 13:34:22

标签: android image nullpointerexception android-volley loader

我正在使用volley库从网络加载图像并获取nullpointer异常。我在Adapter类中使用它。在获取实例或我没有得到任何问题。 getView()抛出nullpointerexception



VolleySingleton.java

package com.w3nuts.babaa.helper;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

public class VolleySingleton {

	private static VolleySingleton instance;
	private RequestQueue requestQueue;
	private static ImageLoader imageLoader;

	private VolleySingleton(Context context) {
		requestQueue = Volley.newRequestQueue(context);

		imageLoader = new ImageLoader(requestQueue,
				new ImageLoader.ImageCache() {
					private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(
							20);

					@Override
					public Bitmap getBitmap(String url) {
						return cache.get(url);
					}

					@Override
					public void putBitmap(String url, Bitmap bitmap) {
						cache.put(url, bitmap);
					}
				});
	}

	public static VolleySingleton getInstance(Context context) {
		if (instance == null) {
			instance = new VolleySingleton(context);
		}
		return instance;
	}

	public RequestQueue getRequestQueue() {
		return requestQueue;
	}

	public static ImageLoader getImageLoader() {
		return imageLoader;
	}
}


ServiceListAdapter.class

package com.w3nuts.babaa.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageContainer;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.NetworkImageView;
import com.w3nuts.babaa.R;
import com.w3nuts.babaa.getterSetter.ServiceDetailGetterSetter;
import com.w3nuts.babaa.helper.VolleySingleton;

public class ServiceListAdapter extends BaseAdapter {

	private Activity activity;
	private LayoutInflater inflater;
	private ArrayList<ServiceDetailGetterSetter> serviceDetailList;

	// Typeface tfRegular, tfLight;

	public ServiceListAdapter(Activity activity,
			ArrayList<ServiceDetailGetterSetter> serviceDetailList) {
		this.activity = activity;
		this.serviceDetailList = serviceDetailList;
	}

	@Override
	public int getCount() {
		return serviceDetailList.size();
	}

	@Override
	public Object getItem(int position) {
		return serviceDetailList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private class ViewHolder {
		RatingBar ratingBar;
		TextView name;
		TextView location;
		TextView rating;
		NetworkImageView image;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;

		View view = convertView;
		if (convertView == null) {
			inflater = activity.getLayoutInflater();
			convertView = inflater.inflate(R.layout.service_list_item, null);

			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.location = (TextView) convertView
					.findViewById(R.id.location);
			holder.rating = (TextView) convertView
					.findViewById(R.id.rating_text);
			holder.image = (NetworkImageView) convertView
					.findViewById(R.id.task_image);
			holder.ratingBar = (RatingBar) convertView
					.findViewById(R.id.rating_bar);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.name.setText(serviceDetailList.get(position).getTitle());
		holder.location.setText(serviceDetailList.get(position).getLocation());
		holder.rating.setText(serviceDetailList.get(position).getRating() + "");
		holder.ratingBar.setRating((float) serviceDetailList.get(position)
				.getRating());
		// ////VOLLEY IMAGE LOADER
		String url = serviceDetailList.get(position).getImage();
		ImageLoader imageLoader = VolleySingleton.getImageLoader();

		imageLoader.get(url, new ImageListener() {

			public void onErrorResponse(VolleyError error) {
				holder.image.setImageResource(R.drawable.ic_launcher);
			}

			public void onResponse(ImageContainer response, boolean arg1) {
				if (response.getBitmap() != null) {
					holder.image.setImageBitmap(response.getBitmap());
				}
			}
		});

		Log.i("title", serviceDetailList.get(position).getTitle());
		Log.i("location", serviceDetailList.get(position).getLocation());
		Log.i("image", serviceDetailList.get(position).getImage());
		// SET TEXT AND IMAGE HERE

		return convertView;
	}
}



Service_list_item.xml

 <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/task_image"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="centerInside"
            android:src="@drawable/task_demo_image" />
    </LinearLayout>
&#13;
&#13;
&#13;

03-03 18:40:14.909:E / AndroidRuntime(6317):致命异常:主要 03-03 18:40:14.909:E / AndroidRuntime(6317):java.lang.NullPointerException 03-03 18:40:14.909:E / AndroidRuntime(6317):at com.w3nuts.babaa.adapter.ServiceListAdapter.getView(ServiceListAdapter.java:93) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.AbsListView.obtainView(AbsListView.java:2177) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.ListView.makeAndAddView(ListView.java:1840) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.ListView.fillDown(ListView.java:675) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.ListView.fillFromTop(ListView.java:736) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.ListView.layoutChildren(ListView.java:1655) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.AbsListView.onLayout(AbsListView.java:2012) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.View.layout(View.java:14289) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewGroup.layout(ViewGroup.java:4562) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.LinearLayout.layoutHorizo​​ntal(LinearLayout.java:1660) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.LinearLayout.onLayout(LinearLayout.java:1436) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.View.layout(View.java:14289) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewGroup.layout(ViewGroup.java:4562) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.FrameLayout.onLayout(FrameLayout.java:448) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.View.layout(View.java:14289) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewGroup.layout(ViewGroup.java:4562) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.View.layout(View.java:14289) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewGroup.layout(ViewGroup.java:4562) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.FrameLayout.onLayout(FrameLayout.java:448) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.View.layout(View.java:14289) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewGroup.layout(ViewGroup.java:4562) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.FrameLayout.onLayout(FrameLayout.java:448) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.View.layout(View.java:14289) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewGroup.layout(ViewGroup.java:4562) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.FrameLayout.onLayout(FrameLayout.java:448) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.View.layout(View.java:14289) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewGroup.layout(ViewGroup.java:4562) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.View.layout(View.java:14289) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewGroup.layout(ViewGroup.java:4562) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.FrameLayout.onLayout(FrameLayout.java:448) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.View.layout(View.java:14289) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewGroup.layout(ViewGroup.java:4562) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.View.layout(View.java:14289) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewGroup.layout(ViewGroup.java:4562) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.FrameLayout.onLayout(FrameLayout.java:448) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.View.layout(View.java:14289) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewGroup.layout(ViewGroup.java:4562) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.View.layout(View.java:14289) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewGroup.layout(ViewGroup.java:4562) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.widget.FrameLayout.onLayout(FrameLayout.java:448) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.View.layout(View.java:14289) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewGroup.layout(ViewGroup.java:4562) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5481) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.Choreographer.doCallbacks(Choreographer.java:562) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.Choreographer.doFrame(Choreographer.java:532) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.os.Handler.handleCallback(Handler.java:730) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.os.Handler.dispatchMessage(Handler.java:92) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.os.Looper.loop(Looper.java:137) 03-03 18:40:14.909:E / AndroidRuntime(6317):在android.app.ActivityThread.main(ActivityThread.java:5103) 03-03 18:40:14.909:E / AndroidRuntime(6317):at java.lang.reflect.Method.invokeNative(Native Method) 03-03 18:40:14.909:E / AndroidRuntime(6317):at java.lang.reflect.Method.invoke(Method.java:525) 03-03 18:40:14.909:E / AndroidRuntime(6317):at com.andr

1 个答案:

答案 0 :(得分:0)

问题在于您制作了imageLoader static,并且只在VolleySingleton构造函数中初始化它,该构造函数仅在getInstance()期间调用。

设置imageLoader非 - static,并将getImageLoader方法设为非 - static,并将代码替换为如下所示:

ImageLoader imageLoader = VolleySingleton.getInstance(context).getImageLoader()