的ListView适配器不想要设置文本
问题描述:
好,我想从与转接器阵列做简单的ListView填充,所以我必须:的ListView适配器不想要设置文本
公共类MainActivity扩展活动{
String[] dish = {"soup", "rice", "grecha", "potato"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lunch);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.lunch);
ListView lunchLV = (ListView)findViewById(R.id.lunchLV);
cafeAdapter ca = new cafeAdapter();
lunchLV.setAdapter(ca);
}
public class cafeAdapter extends BaseAdapter {
@Override
public int getCount() {
return dish.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.list, null);
TextView title = (TextView) findViewById(R.id.title);
title.setText(dish[i]);
return view;
}
}
}
我也有list.xml文件和lunch.xml文件
有些奇怪的原因title.setText(dish [i]);产生一个错误,并使我的程序崩溃。 我有我的xml文件中的id为“title”的元素,但无论如何java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'。
你有任何想法,我做错了
答
您应该使用
TextView title = (TextView) view.findViewById(R.id.title);
相反:
TextView title = (TextView) findViewById(R.id.title);
我都在同一个文件..菜串只是INSI主要活动。当我试图只是setText(“你好”)我有同样的问题 – Sofi