[英](RecyclerView) Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
I'm getting a null point exception while setting text..设置文本时出现空点异常..
my Fragment for recycler view我的 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();
}
}
my 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);
}
}
my model class我的模型课
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
<?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>
my row_xml我的 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>
Am i missing something.?Please point the mistake i'm doing.我错过了什么。?请指出我正在做的错误。
检查此布局: R.layout.nav_drawer_row
最有可能的, txtPname为空。
Try this:尝试这个:
@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);
}
}
your adapter has R.layout.nav_drawer_row in你的适配器有 R.layout.nav_drawer_row
this method here :这个方法在这里:
@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;
}
do this instead改为这样做
@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;
}
so if your referencing the wrong xml to the viewholder you are bound to get null pointer execption因此,如果您将错误的 xml 引用到视图持有者,您一定会得到空指针执行
do some research for on https://developer.android.com for recyclerview , you will get more ideas on how to use recyclerview在https://developer.android.com 上对 recyclerview 进行一些研究,您将获得有关如何使用 recyclerview 的更多想法
Check if R.layout.nav_drawer_row is the right layout in onCreateViewHolder.检查R.layout.nav_drawer_row是否是 onCreateViewHolder 中的正确布局。
Logically, if the instance of the TextView is null, the findViewById call in the holder returned null.从逻辑上讲,如果 TextView 的实例为 null,则持有者中的 findViewById 调用将返回 null。 One possible cause is trying to inflate a different layout~, by mistake, a one that dosen't have that specific id to be found.
一个可能的原因是试图膨胀一个不同的布局~错误地,一个没有那个特定 id 的布局。 Take a look at the definition in https://developer.android.com/reference/android/app/Activity#findViewById(int)
看看https://developer.android.com/reference/android/app/Activity#findViewById(int)中的定义
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.