在TextView上设置值时出现空指针异常

时间:2017-11-19 00:56:28

标签: java android

我已经浏览了本网站上提供的所有解决方案,以删除此空指针,但没有一个有效,因此我发布了这个问题。

我正在尝试将值设置为TextView,但会返回空指针。

主要活动中的代码:

static class ViewHolder{

    public TextView txtNum;
    public TextView txtProduct;
    public TextView txtUnitPrice;
    public TextView txtQTY;
    public TextView txtStatus;
    public TextView txtUser;
    public TextView txtDate;


}


    public View getView(int position, View convertView, ViewGroup parent){
    View rowView=convertView;
    if(rowView==null) {
        System.out.println("ZZZZZZ IF CONDITION IS TRUE ZZZZZZZZZZZZ");
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(vg, parent, false);
        ViewHolder holder = new ViewHolder();
        holder.txtNum = (TextView) rowView.findViewById(R.id.txtNum);
        holder.txtProduct = (TextView) rowView.findViewById(R.id.txtProduct);
        holder.txtUnitPrice = (TextView) rowView.findViewById(R.id.txtUnitPrice);
        holder.txtQTY = (TextView) rowView.findViewById(R.id.txtQty);
        holder.txtStatus = (TextView) rowView.findViewById(R.id.txtStatus);
        holder.txtUser = (TextView) rowView.findViewById(R.id.txtUser);
        holder.txtDate = (TextView) rowView.findViewById(R.id.txtDate);
        rowView.setTag(holder);
        System.out.println("EVERYTHING HAS BEEN INITIALIZED");
    }
        //String[] item = item_list[position].split("__");
        Inventory row = list.get(position);
        ViewHolder holder =(ViewHolder)rowView.getTag();
        System.out.println("THIS IS WHAT ITEM = " + row.Item);
        System.out.println("THIS IS WHAT PRODUCT = " + row.Product);
        System.out.println("THIS IS WHAT UNIT_PRICE = " + row.Unit_Price);
        System.out.println("THIS IS WHAT QTY = " + row.Qty);
        System.out.println("THIS IS WHAT STATUS = " + row.Status);
        System.out.println("THIS IS WHAT USER = " + row.UserUpdate);
        System.out.println("THIS IS WHAT DATE = " + row.Last_Updated);



        holder.txtNum.setText(row.Item);
        holder.txtProduct.setText(row.Product);
        holder.txtUnitPrice.setText(row.Unit_Price);
        holder.txtQTY.setText(row.Qty);
        holder.txtStatus.setText(row.Status);
        holder.txtUser.setText(row.UserUpdate);
        holder.txtDate.setText(row.Last_Updated);

        return rowView;
}

正在设置文本的类:

XML:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:stretchColumns="*">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        >


        <TextView
            android:id="@+id/txtNum"
            android:layout_column="2"
            android:background="#c0c0c0"
            android:padding="10dp"
            android:layout_marginLeft="-21dp"
            android:text="#"
            android:textSize="10dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/txtProduct"
            android:layout_column="3"
            android:background="#c0c0c0"
            android:padding="10dp"
            android:text="Product"
            android:textSize="10dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/txtUnitPrice"
            android:layout_column="3"
            android:background="#c0c0c0"
            android:padding="10dp"
            android:text="Unit Price"
            android:textSize="10dp"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/txtQTY"
            android:layout_column="3"
            android:background="#c0c0c0"
            android:padding="10dp"
            android:text="Qty"
            android:textSize="10dp"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/txtStatus"
            android:layout_column="3"
            android:background="#c0c0c0"
            android:padding="10dp"
            android:text="Status"
            android:textSize="10dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/txtUser"
            android:layout_column="3"
            android:background="#c0c0c0"
            android:padding="10dp"
            android:text="User"
            android:textSize="10dp"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/txtDate"
            android:layout_column="1"
            android:background="#c0c0c0"
            android:padding="10dp"
            android:text="Date"
            android:textSize="10dp"
            android:textStyle="bold" />







    </TableRow>


</TableLayout>

以下行发生错误:   holder.txtNum.setText(row.Item);

        t1 = "År {}: {:25d}".format(2015, 41231226098)
        t2 = "År {}: {:25d}".format(2016, 3093)

        print(t1, '\n' + t2)  
        # Output:              # It formats nicely here as you can see:
        # År 2015:                      3093                       
        # År 2016:               41231226098

        # Now I update the two text fields... # Something goes wrong now
        self.DPcanvas.itemconfig(self.DP_helpBox_lab, text=label)
        self.DPcanvas.itemconfig(self.DP_helpBox_årxLab, text=t1)
        self.DPcanvas.itemconfig(self.DP_helpBox_åryLab, text=t2)

这个问题与已经被建议作为一种可能的解决方案的不同之处在于,它涉及多个层次并使它们膨胀,我认为这是一个问题

请帮助!

1 个答案:

答案 0 :(得分:0)

rowView = inflater.inflate(vg,parent,false);

将其更改为

rowView = inflater.inflate(R.layout.row_layout, parent, false);

其中row_layout.xml是您发布的XML文件