我在应用程序中编程显示单词,但我有错误:
java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'
我知道我的错误引用了:
中的CustomBaseAadpterholder.txtWord.setText(rowItem.getword())
但我不知道如何解决。
CustomBaseAdapter.java
public class CustomBaseAdapter extends BaseAdapter {
Context context;
private List<item_row_adapter> rowItems;
public CustomBaseAdapter(Context context, List<item_row_adapter> items) {
this.context = context;
this.rowItems = items;
}
private class ViewHolder {
TextView txtWord;
TextView txtMean;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_view, null);
holder = new ViewHolder();
holder.txtWord = (TextView) convertView.findViewById(R.id.word_word);
holder.txtMean = (TextView) convertView.findViewById(R.id.mean_mean);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
item_row_adapter rowItem = (item_row_adapter) getItem(position);
holder.txtWord.setText(rowItem.getword());
holder.txtMean.setText(rowItem.getmean());
return convertView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return rowItems.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return rowItems.indexOf(getItem(position));
}
}
item_row_adapter.java
public class item_row_adapter {
private String word ="";
private String mean = "";
public item_row_adapter(String word, String mean) {
this.word = word;
this.mean = mean;
}
public String getmean() {
return mean;
}
public void setmean(String mean) {
this.mean = mean;
}
public String getword() {
return word;
}
public void setword(String word) {
this.word = word;
}
public String tostring() {
return mean + "\n" + word;
}
}
vocab_1_program.java
public class vocab_1_program extends Activity implements OnItemClickListener {
private ListView listview ;
public static final String[] Word_item = new String[] { "Strawberry", "Banana", "Orange", "Mixed" };
public static final String[] Mean_word = new String[] {
"It is an aggregate accessory fruit",
"It is the largest herbaceous flowering plant", "Citrus Fruit", "Mixed Fruits" };
List<item_row_adapter> rowItems;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vocab_1_layout);
rowItems = new ArrayList<item_row_adapter>();
for (int i = 0; i < Word_item.length; i++) {
item_row_adapter item = new item_row_adapter(Word_item[i],
Mean_word[i]);
rowItems.add(item);
}
listview = (ListView) findViewById(R.id.listview_view);
CustomBaseAdapter adapter = new CustomBaseAdapter(this, rowItems);
listview.setAdapter(adapter);
listView.setOnItemClickListener((android.widget.AdapterView.OnItemClickListener) this);
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast toast = Toast.makeText(getApplicationContext(), "Item "
+ (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}
word_row.xml
<TextView
android:id="@+id/word_word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Header"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/mean_mean"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Mean" />
vocab_1_layout.xml
<ListView
android:id="@+id/listview_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
答案 0 :(得分:5)
你正在为行添加错误的布局。
R.layout.list_view
没有所需的视图。 你在
中拥有它word_row.xml
在您CustomBaseAadpter
下的public View getView(int position, View convertView, ViewGroup parent)
下,您正在给没有所需视图元素的xml充气。
方法findViewById()
返回与id对应的视图实例,并显示在膨胀的布局中。
由于您已经夸大了错误的布局,并且布局没有找到视图,这就是为什么medthod findViewById()
为视图返回null。
请注意这一行 -
holder.txtWord = (TextView) convertView.findViewById(R.id.word_word);
holder.txtMean = (TextView) convertView.findViewById(R.id.mean_mean);
因此holder.txtWord= null and holder.txtMean=null
当你设置调用实例setText()
上的方法holder.txtWord
时,它会抛出NPE。
答案 1 :(得分:4)
您收到错误是因为您已将错误的布局夸大到CustomBaseAdapter
。
您已夸大list_view.xml
而不是word_row.xml
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.word_row, null);
holder = new ViewHolder();
holder.txtWord = (TextView) convertView.findViewById(R.id.word_word);
holder.txtMean = (TextView) convertView.findViewById(R.id.mean_mean);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
item_row_adapter rowItem = (item_row_adapter) getItem(position);
holder.txtWord.setText(rowItem.getword());
holder.txtMean.setText(rowItem.getmean());
return convertView;
}