Android listview适配器不想设置文本
好的,我正在尝试使用适配器从阵列填充简单的listview,因此我有: 公共类MainActivity扩展了活动{Android listview适配器不想设置文本,android,listview,android-adapter,settext,Android,Listview,Android Adapter,Settext,好的,我正在尝试使用适配器从阵列填充简单的listview,因此我有: 公共类MainActivity扩展了活动{ String[] dish = {"soup", "rice", "grecha", "potato"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.lunch)
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文件和sunch.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);
我所有的都在同一个文件中..dish string就在main活动中。当我试图设置text(“hello”)时,我遇到了同样的问题