java.lang.NullPointerException:

我正在尝试将表布局与 recyclerView 一起使用,但它给出了错误:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TableRow.addView(android.view.View)' 在空对象引用上。


有我的适配器类:


public class CompanyAdapter extends RecyclerView.Adapter<CompanyAdapter.bookViewHolder> {


ArrayList<DataClass> arrayList;

TextView deposit;

TextView purchase;

TextView date;

TableRow tableRow;

TableLayout tableLayout;


public CompanyAdapter(ArrayList<DataClass> arrayList){

    this.arrayList = arrayList;

}


@Override

public bookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    Context context = parent.getContext();

    View view = 


 LayoutInflater.

from(context).inflate(R.layout.company_list_item,parent,false);

    return new bookViewHolder(view);

}


@Override

public void onBindViewHolder(bookViewHolder holder, int position) {

    DataClass dataClass = arrayList.get(position);

    holder.bind(dataClass);

    //bookNameTV.setText(dataClass.getBook());

    //authorNameTV.setText(dataClass.getAuthor());



}


@Override

public int getItemCount() {

    return arrayList.size();

}


public class bookViewHolder extends RecyclerView.ViewHolder{




    public bookViewHolder(View itemView) {

        super(itemView);

        deposit = (TextView) itemView.findViewById(R.id.deposit);

        purchase = (TextView) itemView.findViewById(R.id.purchase);

        date = (TextView) itemView.findViewById(R.id.date);

        tableLayout = (TableLayout) 

itemView.findViewById(R.id.maintablelayout);


    }


还有一个问题,有没有更好的方法可以在不使用任何库的情况下将 tableLayout 与 recycler View 一起使用?

谢谢..!


素胚勾勒不出你
浏览 174回答 3
3回答

忽然笑

TableRow是一个实例变量,所以 JAVA 会在运行时用空值初始化它,你不能用空值调用任何方法。您必须将一个对象分配给TableRow引用变量。喜欢:tableRow=new&nbsp;TableRow(context);

米琪卡哇伊

您没有tableRow在任何地方初始化。

DIEA

您必须创建新的 TableRow使用此代码TableRow tableRow=new TableRow(context);&nbsp;tableRow.addView(deposit);&nbsp; &nbsp; tableRow.addView(purchase);&nbsp; &nbsp; tableRow.addView(date);&nbsp; &nbsp; if(tableRow.getParent()!=null){&nbsp; &nbsp; &nbsp; &nbsp; ((ViewGroup)tableRow.getParent()).removeView(tableRow);&nbsp; &nbsp; }&nbsp; &nbsp; tableLayout.addView(tableRow, new TableLayout.LayoutParams(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TableLayout.LayoutParams.FILL_PARENT,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TableLayout.LayoutParams.WRAP_CONTENT));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java