gpt4 book ai didi

java.lang.NullPointerException ListViewAdapter.getView(ListViewAdapter.java :60) on fragment

转载 作者:行者123 更新时间:2023-12-01 13:05:45 24 4
gpt4 key购买 nike

1.这个ListView Adapter集成在fragment上

public class ListViewAdapter extends BaseAdapter {

// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();

public ListViewAdapter(NewsFragment newsFragment,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}



@Override
public int getCount() {
return data.size();
}

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

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

public View getView(final int position, View convertView, ViewGroup parent) {
//protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Declare Variables
TextView productname;
TextView productid;
TextView producttype;
ImageView image;

这是问题 inflater = (LayoutInflater)convertView.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

 enter code here

View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Get the position
resultp = data.get(position);

// Locate the TextViews in listview_item.xml
productname = (TextView) itemView.findViewById(R.id.product);
productid = (TextView) itemView.findViewById(R.id.dateOfInput);
producttype = (TextView) itemView.findViewById(R.id.productprice);

// Locate the ImageView in listview_item.xml
image = (ImageView) itemView.findViewById(R.id.flag);

// Capture position and set results to the TextViews
productname.setText(resultp.get(NewsFragment.PRODUCTNAME));
productid.setText(resultp.get(NewsFragment.PRODUCTPRICE));
producttype.setText(resultp.get(NewsFragment.DATEOFINPUT));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(NewsFragment.IMAGE), image);
// Capture ListView item click
itemView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// Get the position
resultp = data.get(position);
Intent intent = new Intent(context, SingleItemView.class);
// Pass all data rank
intent.putExtra("productname",resultp.get(NewsFragment.PRODUCTNAME));
// Pass all data country
intent.putExtra("productid", resultp.get(NewsFragment.PRODUCTPRICE));
// Pass all data population
intent.putExtra("producttype", resultp.get(NewsFragment.DATEOFINPUT));
// Pass all data flag
intent.putExtra("image", resultp.get(NewsFragment.IMAGE));
// Start SingleItemView Class
context.startActivity(intent);

}
});
return itemView;
}

}

最佳答案

您在初始化LayoutInflatrot时犯了错误

你已经做到了

inflater = (LayoutInflater)convertView.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

这是错误的,因为convertView一开始将为null。

相反,你需要做类似的事情

inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

如果您想使用上下文,您需要在适配器的构造函数中正确初始化它,它当前指向自身。

关于java.lang.NullPointerException ListViewAdapter.getView(ListViewAdapter.java :60) on fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23283365/

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