gpt4 book ai didi

java - 自定义 ListView 适配器有 null NullPointerException 错误

转载 作者:太空狗 更新时间:2023-10-29 15:45:38 26 4
gpt4 key购买 nike

我在应用程序中编程显示字,但我有错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

我知道我的错误是指 CustomBaseAadpter 中的:

holder.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>

最佳答案

您为该行添加了错误的布局。

R.layout.list_view

没有所需的 View 。你正在拥有它

word_row.xml

public View getView(int position, View convertView, ViewGroup parent) 下的 CustomBaseAadpter 中,您正在膨胀没有所需 View 元素的 xml。

findViewById() 方法返回一个与 id 对应的 View 实例,并出现在展开的布局中。由于您膨胀了错误的布局,并且该布局没有找到 View ,这就是方法 findViewById() 为 View 返回 null 的原因。

注意这一行-

    holder.txtWord = (TextView) convertView.findViewById(R.id.word_word);
holder.txtMean = (TextView) convertView.findViewById(R.id.mean_mean);

因此这里 holder.txtWord= null 和 holder.txtMean=null当您设置时调用实例 holder.txtWord 上的方法 setText() 它会抛出 NPE。

关于java - 自定义 ListView 适配器有 null NullPointerException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31625301/

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