使用BaseAdapter的LayoutInflater异常

时间:2015-06-18 10:06:29

标签: java android json baseadapter

我的类扩展了BaseAdapter,我正在使用Fragment但是我在这一行得到 NullPointerException

  this.myInflater = LayoutInflater.from(activity);

我可以知道是什么原因吗?为什么我要面对这个问题?我怎么解决这个问题?

适配器代码:

public class GridViewImageAdapter extends BaseAdapter {

    private Activity _activity;
    private ArrayList<Wallpaper> _wallPArray;

    private LayoutInflater myInflater;

    private ImageLoader imageLoader;
    private Utils utils;
    private ImageLoaderConfiguration config;
    private DisplayImageOptions options;

    public GridViewImageAdapter(Activity activity, ArrayList<Wallpaper> filePaths) {
        this._activity = activity;
        this._wallPArray = filePaths;


        this.myInflater = LayoutInflater.from(activity); // getting NPE


        imageLoader = ImageLoader.getInstance();        
        utils = new Utils(activity);

        //Create configuration for ImageLoader
        config = utils.ImgLoaderConfiguration();

        // Creates display image options for custom display task
        options = utils.DisplayImgOptions();

        // Initialize ImageLoader with created configuration. Do it once.
        imageLoader.init(config);
    }

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

        if (convertView == null) {
            convertView = myInflater.inflate(R.layout.layout_gridview_image, null);
            holder = new ViewHolder();
            holder.preview = (ImageView) convertView.findViewById(R.id.grid_item_image);

            convertView.setTag(holder);

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

logcat的:

java.lang.NullPointerException
            at android.view.LayoutInflater.from(LayoutInflater.java:211)
            at com.test.data.GridViewImageAdapter.<init>(GridViewImageAdapter.java:42)
            at com.test.data.PhotosFragment.jsonTaskComplete(PhotosFragment.java:112)
            at com.test.data.PhotosFragment$JsonDownloaderTask.onPostExecute(PhotosFragment.java:149)
            at com.test.data.PhotosFragment$JsonDownloaderTask.onPostExecute(PhotosFragment.java:118)
            at android.os.AsyncTask.finish(AsyncTask.java:632)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5061)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:603)
            at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

  

我可以知道是什么原因吗?为什么我要面对这个问题?我怎么能够   解决这个问题?

原因是,activity为空。当您拥有有效的Context时,请检查您是否正在实例化适配器。例如。在onCreate内。由于您仅使用“活动”来检索LayoutInflater,因此您也只能传递LayoutInflater的实例

答案 1 :(得分:0)

尝试做以下事情:

this.myInflater = LayoutInflater.from(getActivity());

如果您在片段中,这是获取有效上下文的方法

答案 2 :(得分:0)

如果您使用片段,请尝试此代码

this.myInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

public GridViewImageAdapter(Context context,ArrayList filePaths){}

在你的代码中使用上下文而不是活动