(RecyclerView)尝试在空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'

时间:2016-10-18 13:03:07

标签: java android android-recyclerview

我在设置文本时遇到了零点异常..

我的Fragment for recycler view

public class Product_Manual_Frag extends Fragment implements View.OnClickListener {

    //Database
    DBHelper db;
    Cursor myCursor = null;
    View rootView;
    // Cursor myCursor = null;
    List<RecyclerModel> data = fill_with_data();


    Pos_Ability_Activity main_Activity;

    public Product_Manual_Frag() {
        // Required empty public constructor
    }

    @Override
    public void onClick(View v) {

    }


    private List<RecyclerModel> fill_with_data() {
        List<RecyclerModel> data=new ArrayList<>();
        data.add(new RecyclerModel("Item1","10,000","2000","$"));
        data.add(new RecyclerModel("Item1","10,000","2000","$"));
        data.add(new RecyclerModel("Item1","10,000","2000","$"));
        data.add(new RecyclerModel("Item1","10,000","2000","$"));
        return data;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       rootView = inflater.inflate(R.layout.fragment_poduct_manual, container, false);

        RecyclerView recycler=(RecyclerView)rootView.findViewById(R.id.my_recycler_view);
        RecyclerViewAdapter adapter=new RecyclerViewAdapter(data,getActivity());
        recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        recycler.setAdapter(adapter);



        // Inflate the layout for this fragment
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

我的Recycleradapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.myViewholder> {
    List<RecyclerModel> data = Collections.emptyList();
    private LayoutInflater inflater;
    private Context context;

    public RecyclerViewAdapter(List<RecyclerModel> list, Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.data = list;
    }

    // Usually involves inflating a layout from XML and returning the holder
    @Override
    public myViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.nav_drawer_row, parent, false);
        RecyclerViewAdapter.myViewholder holder = new RecyclerViewAdapter.myViewholder(view);
        return holder;
    }

    public void delete(int position) {
        data.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public void onBindViewHolder(myViewholder holder, int position) {


        holder.txtPname.setText(data.get(position).Itemname);  //Getting  exception here

    } 




    @Override
    public int getItemCount() {
        return data.size();
    }




    class myViewholder extends RecyclerView.ViewHolder {
        RelativeLayout rl;
        TextView txtPname, txtorgprice, txtdisprice, txtcursym;
        CheckBox chkbx;

        public myViewholder(View itemView) {
            super(itemView);
            rl = (RelativeLayout) itemView.findViewById(R.id.relativeLayout1);
            txtPname = (TextView) itemView.findViewById(R.id.txtPName);
            txtorgprice = (TextView) itemView.findViewById(R.id.txtPOrgPrice);
            txtdisprice = (TextView) itemView.findViewById(R.id.txtPDisPrice);
            txtcursym = (TextView) itemView.findViewById(R.id.lblCurSym);
            chkbx = (CheckBox) itemView.findViewById(R.id.chkBox);

        }
    }

我的模特课

public class RecyclerModel {

    public String Itemname;
    public String ItempOrigrice;
    public String ItemdiscPrice;
    public String curSym;



    public RecyclerModel(String name, String orgprice, String DiscPrice, String currency) {
        this.curSym = currency;
        this.Itemname = name;
        this.ItempOrigrice = orgprice;
        this.ItemdiscPrice = DiscPrice;

    }

my fragment xml

    <?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/app_bg">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />
</RelativeLayout>

我的row_xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">


    <ImageView
        android:id="@+id/imgIcon"
        android:background="@drawable/cashback"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true" />

    <TextView
        android:id="@+id/txtPName"
        android:text="Item name  sample"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/imgIcon" />

    <TextView
        android:id="@+id/txtPOrgPrice"
        android:text="10,000 "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtPName"
        android:layout_toRightOf="@+id/imgIcon" />

    <TextView
        android:id="@+id/lblCurSym"
        android:text="$"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtPOrgPrice"
        android:layout_toRightOf="@+id/imgIcon" />

    <TextView
        android:id="@+id/txtPDisPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtPOrgPrice"
        android:layout_toRightOf="@+id/lblCurSym"
        android:paddingBottom="2dp"
        android:paddingRight="2dp"
        android:paddingTop="2dp"
        android:text="2,000 "
        android:textColor="#83410e"
        android:textSize="14sp" />

    <CheckBox
        android:id="@+id/chkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true" />


</RelativeLayout>

我错过了什么。请指出我正在做的错误。

4 个答案:

答案 0 :(得分:0)

检查此布局:R.layout.nav_drawer_row 最有可能的是, txtPname 为空。

答案 1 :(得分:0)

试试这个:

@Override
public myViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView= inflater.inflate(R.layout.nav_drawer_row, parent, false);
RecyclerViewAdapter.myViewholder holder = new RecyclerViewAdapter.myViewholder(itemView);
    holder.rl = (RelativeLayout) itemView.findViewById(R.id.relativeLayout1);
    holder .txtPname = (TextView) itemView.findViewById(R.id.txtPName);
    holder .txtorgprice = (TextView) itemView.findViewById(R.id.txtPOrgPrice);
    holder .txtdisprice = (TextView) itemView.findViewById(R.id.txtPDisPrice);
    holder .txtcursym = (TextView) itemView.findViewById(R.id.lblCurSym);
    holder .chkbx = (CheckBox) itemView.findViewById(R.id.chkBox);
return holder;
}
class myViewholder extends RecyclerView.ViewHolder {
RelativeLayout rl;
TextView txtPname, txtorgprice, txtdisprice, txtcursym;
CheckBox chkbx;

public myViewholder(View itemView) {
    super(itemView);
}
}

答案 2 :(得分:0)

你的适配器在

中有R.layout.nav_drawer_row

此方法:

@Override
public myViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.nav_drawer_row, parent, false);
    RecyclerViewAdapter.myViewholder holder = new RecyclerViewAdapter.myViewholder(view);
    return holder;
}

改为执行此操作

@Override
public myViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.my_row_xml, parent, false);
    RecyclerViewAdapter.myViewholder holder = new RecyclerViewAdapter.myViewholder(view);
    return holder;
}

因此,如果您向视图引用错误的xml,则必须获取空指针执行

对于https://developer.android.com进行一些关于recyclerview的研究,您将获得有关如何使用recyclerview的更多想法

答案 3 :(得分:0)

检查 R.layout.nav_drawer_row 在onCreateViewHolder中是否正确。