自定义列表视图适配器有 null NullPointerException 错误

Custom list View adapter have null NullPointerException error

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

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

没有所需的视图。 您正在

word_row.xml

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

方法findViewById() returns 一个与id对应的视图实例,存在于膨胀的布局中。 由于您膨胀了错误的布局,并且该布局没有找到视图,这就是为什么视图的方法 findViewById() returns 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 当您设置时,您在实例 holder.txtWord 上调用方法 setText() 它会抛出 NPE。

您收到错误消息是因为您在 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;
}