gpt4 book ai didi

java - 使用 BaseAdapter 的 LayoutInflater 异常

转载 作者:行者123 更新时间:2023-12-01 11:24:49 27 4
gpt4 key购买 nike

我的类扩展了 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)

最佳答案

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

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

的实例

关于java - 使用 BaseAdapter 的 LayoutInflater 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30912370/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com