Android通用图像加载器(nostra13):获取nullPointerException

时间:2013-05-12 09:05:10

标签: android bitmap gallery

看起来设置Bitmap有一些问题,但我不知道为什么。这是我第一次使用这个库,所以非常感谢每一个帮助!我遇到错误的行是我做setImageBitmap(myBitmap)的地方。 这是我的适配器的代码:

public class CustomGalleryAdapter extends BaseAdapter {

private List<String> imageUrls;
private Context context;

// Variabili per Android Universal Image Loader
private ImageLoader imageLoader;

public CustomGalleryAdapter(Context context, Context appContext, List<String> imageUrls){
    this.imageUrls = imageUrls;
    this.context = context;

    // Inizializza Android Universal Image Loader
    imageLoader = ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration.createDefault(appContext));
}

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

@Override
public Object getItem(int position) {
    return "file://"+imageUrls.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent){

    // Classe che contiene l'imageView
    ViewHolder holder;

    // Se viene creato e non "riutilizzato"
    if (convertView == null) {
        holder = new ViewHolder();
        holder.imageView = new ImageView(context);      
    } 
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    // Decodifica il Bitmap
    Bitmap myBitmap = BitmapFactory.decodeFile(imageUrls.get(position));

    // Setta il bitmap nella imageView
    holder.imageView.setImageBitmap(myBitmap);
    holder.imageView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, GridView.LayoutParams.WRAP_CONTENT));

    // Ottiene l'URL formattato per la libreria
    String imageUrl = "file://"+imageUrls.get(position);

    // Carica e visualizza l'immagine
    imageLoader.displayImage(imageUrl, holder.imageView);

    return holder.imageView;
}   

// Classe "wrapper" per l'imageView
static class ViewHolder {
    ImageView imageView;
}
}

这是LogCat:

05-12 10:55:23.841: E/AndroidRuntime(8691): FATAL EXCEPTION: main
05-12 10:55:23.841: E/AndroidRuntime(8691): java.lang.NullPointerException
05-12 10:55:23.841: E/AndroidRuntime(8691):     at it.romagnoli.andrea.myrehearsals.adapters.CustomGalleryAdapter.getView(CustomGalleryAdapter.java:69)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.widget.AbsListView.obtainView(AbsListView.java:2143)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.widget.GridView.makeAndAddView(GridView.java:1341)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.widget.GridView.makeRow(GridView.java:341)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.widget.GridView.fillDown(GridView.java:283)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.widget.GridView.fillFromTop(GridView.java:417)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.widget.GridView.layoutChildren(GridView.java:1229)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.widget.AbsListView.onLayout(AbsListView.java:1994)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.View.layout(View.java:14008)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.View.layout(View.java:14008)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.View.layout(View.java:14008)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.View.layout(View.java:14008)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.View.layout(View.java:14008)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.Choreographer.doFrame(Choreographer.java:532)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.os.Handler.handleCallback(Handler.java:725)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.os.Looper.loop(Looper.java:137)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at java.lang.reflect.Method.invokeNative(Native Method)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at java.lang.reflect.Method.invoke(Method.java:511)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-12 10:55:23.841: E/AndroidRuntime(8691):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

如果convertView不是null,则ViewHolder没有getTag()返回ViewHolder设置{ {1}}代码中{1}}的代码中的{1}}。另外,如果您只使用convertView,则实际上不需要ImageViewViewHolder基本上会引用ViewHolder,然后是ImageView将被设置为ViewHolder方法返回的视图上的标记,该方法是持有者指向的确切getView()。只需使用ImageView