0

看起来设置位图有问题,但我不知道为什么。这是我第一次使用这个库,所以非常感谢每一个帮助!我得到错误的那一行是我做 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)
4

1 回答 1

1

如果convertView不是,则null您没有 aViewHolder可以返回getTag()(您没有在代码中的任何位置将 设置ViewHolder为标签convertView)。此外,如果你只使用 anImageView你真的不需要 a ViewHolder,基本上ViewHolder将引用 an ImageView,那么ViewHolder将被设置为getView()方法返回的视图上的标签,这正是ImageView持有者指向的。只需使用ImageView.

于 2013-05-12T09:15:05.613 回答