0
extends SherlockFragment{

我正在使用夏洛克片段和滑动菜单开发应用程序。在我的第一个框架布局中,我必须使用来自 url 的图像显示网格视图。我决定使用Universal Image Loader

我可以获取 json 和所有值。但是在 ImageAdapter 类中将适配器设置为网格视图时,我得到了 NULL 指针异常。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final View v = inflater.inflate(R.layout.videos_layout, container,
            false);
    this.setRetainInstance(true);
    gridView = (GridView) v.findViewById(R.id.gridview);
     imageLoader = ImageLoader.getInstance();
    return v;

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

int i = 0;
    for (HashMap<String, String> map : VideoItems) {
        imageUrls[i] = map.get(TAG_THUMNAIL);
        System.out.println(imageUrls[i]);
        i++;

    }
    defaultOptions = new DisplayImageOptions.Builder().cacheInMemory()
            .cacheOnDisc().build();

    config = new ImageLoaderConfiguration.Builder(getActivity()
            .getApplicationContext()).defaultDisplayImageOptions(
            defaultOptions).build();
     imageLoader.init(config);
    gridView.setAdapter(new ImageAdapter(getActivity()));
    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
        }
    });
    this.gridView.setOnScrollListener(new PauseOnScrollListener(
            imageLoader, true, true));
}

public class ImageAdapter extends BaseAdapter {

    private Context mContext;
    public ImageAdapter(Context ctx){
        mContext=ctx;
    }
    @Override
    public int getCount() {
        return imageUrls.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    public class ViewHolder{
        ImageView imageView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=new ViewHolder();
        Log.v("hi", "hhdsjf");
        if (convertView == null) {
               convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.video_layout_item, parent, false);
            holder.imageView= (ImageView) convertView
                        .findViewById(R.id.image);

            convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }
        try {
            imageLoader.displayImage(imageUrls[position], holder.imageView, defaultOptions);

            } catch (Exception e) {
                e.printStackTrace();
            }

            return convertView;
    }
}

日志猫错误:

03-12 15:37:47.164: E/AndroidRuntime(9671): java.lang.NullPointerException

03-12 15:37:47.164: E/AndroidRuntime(9671): 在 com.slidingmenu.example.fragments.VideosFragment.onActivityCreated(VideosFragment.java:112) 03-12 15:37:47.164: E/AndroidRuntime(9671) : 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:891) 03-12 15:37:47.164: E/AndroidRuntime(9671): 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager .java:1080) 03-12 15:37:47.164: E/AndroidRuntime(9671): 在 android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622) 03-12 15:37:47.164: E /AndroidRuntime(9671): 在 android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416) 03-12 15:37:47.164: E/AndroidRuntime(9671): 在 android.support.v4.app。 FragmentManagerImpl$1.run(FragmentManager.java:420) 03-12 15:37:47.164: E/AndroidRuntime(9671): 在 android.os。Handler.handleCallback(Handler.java:587) 03-12 15:37:47.164: E/AndroidRuntime(9671): 在 android.os.Handler.dispatchMessage(Handler.java:92) 03-12 15:37:47.164: E/AndroidRuntime(9671): 在 android.os.Looper.loop(Looper.java:130) 03-12 15:37:47.164: E/AndroidRuntime(9671): 在 android.app.ActivityThread.main(ActivityThread.java :3687) 03-12 15:37:47.164: E/AndroidRuntime(9671): at java.lang.reflect.Method.invokeNative(Native Method) 03-12 15:37:47.164: E/AndroidRuntime(9671): at java.lang.reflect.Method.invoke(Method.java:507) 03-12 15:37:47.164: E/AndroidRuntime(9671): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java :867) 03-12 15:37:47.164: E/AndroidRuntime(9671): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 03-12 15:37:47.164: E/AndroidRuntime (9671):在 dalvik.system。NativeStart.main(本机方法)

我找不到我错过初始化某些东西的地方。,在此先感谢。,

4

1 回答 1

0

试试这个..

1.默认情况下不启用缓存。如果您希望加载的图像将缓存在内存和/或磁盘上,那么您应该以这种方式在 DisplayImageOptions 中启用缓存:

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
    .cacheInMemory()
    .cacheOnDisc()
    .build();

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
         .defaultDisplayImageOptions(defaultOptions)
         .build();
ImageLoader.getInstance().init(config);

在您的适配器中更改它..

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context ctx) {
    mContext = ctx;
}

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

@Override
public Object getItem(int position) {
    return null;
}

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

    ImageView imageView;

}

if (convertView == null) {
   convertView = LayoutInflater.from(mContext).inflate(
            R.layout.video_layout_item, parent, false);
holder.imageView= (ImageView) convertView
            .findViewById(R.id.imageViewid);

convertView.setTag(holder);

} else {
    holder = (ViewHolder) convertView.getTag();
}

  try {
imageLoader.displayImage(imageUrls[position], holder.imageView, defaultOptions);

} catch (Exception e) {
    e.printStackTrace();
}

return convertView;

试试这个来设置适配器

ImageAdapter adapter = new ImageAdapter(this);
            gridView.setAdapter(adapter);
于 2013-03-12T08:47:45.200 回答