以片段形式显示从url到gridview的图像

时间:2013-03-12 08:28:07

标签: android actionbarsherlock universal-image-loader

extends SherlockFragment{

我正在使用sherlock片段和滑动菜单开发应用程序。在我的第一个frament布局中,我必须使用来自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;
    }
}
  

LOG Cat错误:

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

03-12 15:37:47.164:E / AndroidRuntime(9671):at 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):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 03-12 15:37:47.164:E / AndroidRuntime(9671):at dalvik.system.NativeStart.main(Native Method)

  

我无法找到我错过初始化的地方。,   在此先感谢。

1 个答案:

答案 0 :(得分: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);