使用 BaseAdapter 的 LayoutInflater 异常

LayoutInflater Exception using BaseAdapter

我的 class 扩展了 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.access0(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)

May I know what is the reason ? Why i am facing this issue ? How can i resolve this ?

原因是,activity 为空。当您有一个有效的 Context 时,请检查您是否正在实例化适配器。例如。 onCreate 里面。由于您仅使用 Activity 来检索 LayoutInflater,因此您也可以仅传递 LayoutInflater

的一个实例

尝试做以下事情:

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

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

如果你使用片段然后试试这个代码

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

public GridViewImageAdapter(上下文上下文,ArrayList 文件路径){}

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