[英]RecyclerView: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
[英](RecyclerView) Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
設置文本時出現空點異常..
我的 Fragment 用於回收者視圖
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();
}
}
我的回收適配器
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;
}
我的片段 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>
我錯過了什么。?請指出我正在做的錯誤。
檢查此布局: R.layout.nav_drawer_row
最有可能的, txtPname為空。
嘗試這個:
@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);
}
}
你的適配器有 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 的更多想法
檢查R.layout.nav_drawer_row是否是 onCreateViewHolder 中的正確布局。
從邏輯上講,如果 TextView 的實例為 null,則持有者中的 findViewById 調用將返回 null。 一個可能的原因是試圖膨脹一個不同的布局~錯誤地,一個沒有那個特定 id 的布局。 看看https://developer.android.com/reference/android/app/Activity#findViewById(int)中的定義
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.