我有一个奇怪的问题,似乎无法找到导致它的原因。我的ArrayAdapter中的每个视图组件都位于getView方法中,除了我的按钮,它导致NullPointerException。
适配器:
public class ItemAdapter extends ArrayAdapter<Item>{
private Context context;
private int resource;
private List<Item> list;
public ItemAdapter(Context context, int resource, List<Item> list)
{
super(context, resource, list);
this.context = context;
this.resource = resource;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
convertView = LayoutInflater.from(context).inflate(resource, parent,false);
TextView name = (TextView) convertView.findViewById(R.id.itemName);
ImageView itemImg = (ImageView) convertView.findViewById(R.id.itemImg);
Button addToCart = (Button) convertView.findViewById(R.id.button1);
final Item item = list.get(position);
name.setText(item.getName());
UrlImageViewHelper.setUrlDrawable(itemImg, item.getPicture());
addToCart.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view)
{
Toast.makeText(context, "Clicked", Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
使用listview的片段
@InjectView(R.id.itemList) private ListView listView;
@Inject private ItemDao dao;
private ArrayAdapter<Item> adapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_shop, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
dao.open();
List<Item> items = dao.findAll();
dao.close();
adapter = new ItemAdapter(getActivity(),R.layout.list_product_item, items);
listView.setAdapter(adapter);
}
list_product_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp" >
<ImageView
android:id="@+id/itemImg"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="5dp"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/itemStoreName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
现在对convertView.findViewById(R.id.button1)的调用返回一个null实例。造成这种情况的原因是什么?如何解决?
答案 0 :(得分:0)
你有正确的布局名称吗?
adapter = new GroceryItemAdapter(getActivity(), R.layout.list_textview ,items);
VS
list_product_item
(我假设这是 R.layout.list_product_item 的文件名)
答案 1 :(得分:0)
不是使用from方法获取布局inflater而是尝试在构造函数中初始化inflater。
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
getView 中的
convertView = mInflater.inflate(your layout, null);