我的logcat中有一个NullPointerException指向'holder.imageLoader.DisplayImage(resultp.get(BookScreen.KEY_THUMB_URL),holder.thumb_image); '在类LazyAdapter 中。为什么将null传入该行?
public class LazyAdapter扩展了BaseAdapter {
Context context;
HashMap<String, String> resultp = new HashMap<String, String>();
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public LazyAdapter(Context contextActivity, ArrayList<HashMap<String, String>> d) {
this.context = contextActivity;
data = d;
imageLoader = new ImageLoader(context);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
static class ViewHolder {
TextView id, title, description, bookingDate, bookingTime;
ImageView thumb_image;
ImageLoader imageLoader;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
resultp = data.get(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.book_list_row, parent, false);
holder = new ViewHolder();
holder.id = (TextView) convertView.findViewById(R.id.id);
holder.title = (TextView) convertView.findViewById(R.id.menu_name);
holder.description = (TextView) convertView.findViewById(R.id.address);
holder.bookingDate = (TextView) convertView.findViewById(R.id.book_date);
holder.bookingTime = (TextView) convertView.findViewById(R.id.book_time);
holder.thumb_image = (ImageView) convertView.findViewById(R.id.list_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.id.setText(resultp.get(BookScreen.KEY_ID));
holder.title.setText(resultp.get(BookScreen.KEY_TITLE));
holder.description.setText(resultp.get(BookScreen.KEY_ADDRESS));
holder.bookingDate.setText(resultp.get(BookScreen.KEY_DATE));
holder.bookingTime.setText(resultp.get(BookScreen.KEY_TIME));
holder.imageLoader.DisplayImage(resultp.get(BookScreen.KEY_THUMB_URL), holder.thumb_image); //NULLPOINTEREXCEPTION HERE
return convertView;
}
}
与函数DisplayImage相关联的ImageLoader类
:public class ImageLoader {
MemoryCache memoryCache=new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
public ImageLoader(Context context){
fileCache=new FileCache(context);
executorService=Executors.newFixedThreadPool(5);
}
final int stub_id = R.drawable.no_image;
public void DisplayImage(String url, ImageView imageView)
{
imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url);
if(bitmap!=null)
imageView.setImageBitmap(bitmap);
else
{
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
private void queuePhoto(String url, ImageView imageView)
{
PhotoToLoad p=new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url)
{
File f=fileCache.getFile(url);
//from SD cache
Bitmap b = decodeFile(f);
if(b!=null)
return b;
//from web
try {
Bitmap bitmap=null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex){
ex.printStackTrace();
return null;
}
}
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
//Task for the queue
private class PhotoToLoad
{
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i){
url=u;
imageView=i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad){
this.photoToLoad=photoToLoad;
}
@Override
public void run() {
if(imageViewReused(photoToLoad))
return;
Bitmap bmp=getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if(imageViewReused(photoToLoad))
return;
BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
Activity a=(Activity)photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
boolean imageViewReused(PhotoToLoad photoToLoad){
String tag=imageViews.get(photoToLoad.imageView);
if(tag==null || !tag.equals(photoToLoad.url))
return true;
return false;
}
//Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable
{
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
public void run()
{
if(imageViewReused(photoToLoad))
return;
if(bitmap!=null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(stub_id);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:src="@drawable/logo"
android:layout_height="50dip"
/>
</LinearLayout>
<!-- Menu name -->
<TextView
android:id="@+id/menu_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:text="@string/book_name"
android:textColor="#040404"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<!-- Description -->
<TextView
android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/thumbnail"
android:layout_below="@id/menu_name"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:text="@string/book_address"
android:textColor="#343434"
android:textSize="10sp"
tools:ignore="SmallSp" />
<!-- Price -->
<TextView
android:id="@+id/book_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/menu_name"
android:layout_marginRight="5dip"
android:gravity="right"
android:text="@string/book_date"
android:textColor="#10bcc9"
android:textSize="10sp"
android:textStyle="bold"
tools:ignore="SmallSp" />
<TextView
android:id="@+id/book_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/book_date"
android:layout_centerVertical="true"
android:gravity="right"
android:text="@string/book_time"
android:textColor="#10bcc9"
android:textSize="10sp"
android:textStyle="bold"
tools:ignore="SmallSp" />
<TextView
android:id="@+id/id"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
> </TextView>
</RelativeLayout>
logcat的:
12-02 12:30:33.210: E/AndroidRuntime(890): FATAL EXCEPTION: main
12-02 12:30:33.210: E/AndroidRuntime(890): Process: com.example.clinicbooker, PID: 890
12-02 12:30:33.210: E/AndroidRuntime(890): java.lang.NullPointerException
12-02 12:30:33.210: E/AndroidRuntime(890): at com.example.clinicbooker.LazyAdapter.getView(LazyAdapter.java:75)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.widget.ListView.onMeasure(ListView.java:1175)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.View.measure(View.java:16458)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.View.measure(View.java:16458)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.View.measure(View.java:16458)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-02 12:30:33.210: E/AndroidRuntime(890): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.View.measure(View.java:16458)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-02 12:30:33.210: E/AndroidRuntime(890): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.View.measure(View.java:16458)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.Choreographer.doFrame(Choreographer.java:532)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.os.Handler.handleCallback(Handler.java:733)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.os.Handler.dispatchMessage(Handler.java:95)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.os.Looper.loop(Looper.java:137)
12-02 12:30:33.210: E/AndroidRuntime(890): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-02 12:30:33.210: E/AndroidRuntime(890): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 12:30:33.210: E/AndroidRuntime(890): at java.lang.reflect.Method.invoke(Method.java:515)
12-02 12:30:33.210: E/AndroidRuntime(890): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-02 12:30:33.210: E/AndroidRuntime(890): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-02 12:30:33.210: E/AndroidRuntime(890): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
holder.imageLoader
方法中 Initialized
不是getView
。
holder
是instance
ViewHolder
的{{1}}和ViewHolder类Class
中的imageLoader。
答案 1 :(得分:0)
想出来。
imageLoader.DisplayImage(resultp.get(BookScreen.KEY_THUMB_URL), holder.thumb_image);
而不是
holder.imageLoader.DisplayImage(resultp.get(BookScreen.KEY_THUMB_URL), holder.thumb_image);