[英]Error in RecyclerViewAdapter's onBindViewHolder() Method
Guys it could seems similar to you like other NullPionterException Questions and before mark it as duplicate please read it and try to understand my problem first and point it out where i am wrong. 伙计们,您可能会像其他NullPionterException问题一样与您相似,在将其标记为重复之前,请先阅读并尝试先了解我的问题,然后指出我的问题所在。
Actually my app is accessing some JOSN data then parse it and make a POJO object and put that object in some ArrayList<SchemesHitsPojoModel> schemesListHitsWise
then i send this ArrayList to my RecyclerViewAdapter Class and then displaying it in RecyclerView. 实际上,我的应用程序正在访问一些JOSN数据,然后解析它并创建一个POJO对象,然后将该对象放入某些
ArrayList<SchemesHitsPojoModel> schemesListHitsWise
然后将该ArrayList发送到我的RecyclerViewAdapter类,然后在RecyclerView中显示它。
But inside my onBindViewHolder
method when i try to set some text on schemeName then it gives me following error. 但是在我的
onBindViewHolder
方法中,当我尝试在schemeName上设置一些文本时,它给了我以下错误。 Help me guys i am trying to resolve this problem form last couple of days and read lots of answers but still this error remains. 帮帮我,我最近几天试图解决此问题,并阅读了很多答案,但仍然存在此错误。
LogCat LogCat
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.dovezeal.gapp.adapters.HitsOnSchemesDataDisplayAdapter.onBindViewHolder(HitsOnSchemesDataDisplayAdapter.java:71)
at com.dovezeal.gapp.adapters.HitsOnSchemesDataDisplayAdapter.onBindViewHolder(HitsOnSchemesDataDisplayAdapter.java:21)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5277)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5310)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4568)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1371)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at c
MyAdapter File MyAdapter文件
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
MyViewHolder viewHolder;
switch (success){
case(0):
view = layoutInflater.inflate(R.layout.custom_no_record_found_msg_screen, parent, false);
break;
case(1):
view = layoutInflater.inflate(R.layout.custom_recyclerview_row_each_scheme_hits, parent,false);
}
viewHolder = new MyViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
SchemesHitsPojoModel firstObject = schemesListHitsWise.get(0);
success = firstObject.getSuccess();
if(success == 0 ){
Log.d("SORRY!, "," We don't have any records to show you.");
}
else{
SchemesHitsPojoModel currentSchemeRow = schemesListHitsWise.get(position);
holder.schemeName.setText(currentSchemeRow.getSchemeName());
int langId = currentSchemeRow.getLanguage_id();
switch(langId){
case(1):
holder.languageName.setText("Punjabi");
break;
case(2):
holder.languageName.setText("English");
}
holder.schemeViewDate.setText(currentSchemeRow.getSchemeViewDate());
holder.schemeHits.setText(String.valueOf(currentSchemeRow.getSchemeHits()));
holder.viewIcon.setImageResource(R.drawable.ic_remove_red_eye_black_18dp);
//holder.totalHitsOnThisScheme.setText(currentSchemeRow.getSchemeTotalHitsCount());
}
}
// MyViewHolder
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView schemeName, schemeViewDate, languageName, schemeHits,totalHitsOnThisScheme;
ImageView viewIcon;
public MyViewHolder(View itemView) {
super(itemView);
schemeName = (TextView) itemView.findViewById(R.id.hitsOnSchemeName);
languageName = (TextView) itemView.findViewById(R.id.hitsOnLangName);
viewIcon = (ImageView) itemView.findViewById(R.id.hitsOnViewIcon);
schemeViewDate = (TextView) itemView.findViewById(R.id.hitsOnSchemeViewDate);
schemeHits = (TextView) itemView.findViewById(R.id.hitsOnSchemeHits);
//totalHitsOnThisScheme = (TextView) itemView.findViewById(R.id.totalHitsOnThisScheme);
}
}
XML File of RecyclerView's Custom Row RecyclerView的自定义行的XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<GridLayout
android:id="@+id/gridLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/hitsOnSchemeName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="0"
android:width="270dp"
android:text="Name of the Scheme"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000"
android:textSize="14dp" />
<ImageView
android:id="@+id/hitsOnViewIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="0"
android:src="@drawable/ic_remove_red_eye_black_18dp" />
<TextView
android:id="@+id/hitsOnSchemeHits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="left|center"
android:layout_marginLeft="3dp"
android:layout_row="0"
android:text="5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="12dp" />
<TextView
android:id="@+id/hitsOnLangName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="1"
android:width="270dp"
android:text="Language of scheme"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="10dp" />
<TextView
android:id="@+id/hitsOnSchemeViewDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_columnSpan="2"
android:layout_row="1"
android:text="1989-12-09"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="12dp" />
</GridLayout>
</RelativeLayout>
i think you problem is here 我认为你的问题在这里
holder.schemeName.setText(currentSchemeRow.getSchemeName());
change this to 更改为
holder.schemeName.setText(currentSchemeRow.get(position).getSchemeName()
); holder.schemeName.setText(currentSchemeRow.get(position).getSchemeName()
);
move 移动
SchemesHitsPojoModel currentSchemeRow = schemesListHitsWise.get(position);
out of else method 别的方法
You need to change, 你需要改变
There are two chances to get this ERROR. 有两次机会得到此错误。
1.Change your 1.改变你的
holder.schemeViewDate.setText(currentSchemeRow.getSchemeViewDate());
holder.schemeHits.setText(String.valueOf(currentSchemeRow.getSchemeHits()));
TO 至
holder.schemeViewDate.setText(currentSchemeRow.get(position).getSchemeViewDate());
holder.schemeHits.setText("" + currentSchemeRow..get(position).getSchemeHits());
currentSchemeRow should be Arraylist of your model where you have get and set data, and you have to get value from get(position) that is your selected position. currentSchemeRow应该是您的模型的Arraylist,您可以在其中获取和设置数据,并且必须从作为所选位置的get(position)获取值。
schemeName = (TextView) itemView.findViewById(R.id.hitsOnSchemeName);
languageName = (TextView) itemView.findViewById(R.id.hitsOnLangName);
viewIcon = (ImageView) itemView.findViewById(R.id.hitsOnViewIcon);
schemeViewDate=(TextView)itemView.findViewById(R.id.hitsOnSchemeViewDate);
schemeHits = (TextView) itemView.findViewById(R.id.hitsOnSchemeHits);
your problem is in onCreateViewHolder 你的问题在onCreateViewHolder
switch (success){
case(0):
view = layoutInflater.inflate(R.layout.custom_no_record_found_msg_screen, parent, false);
break;
case(1):
view = layoutInflater.inflate(R.layout.custom_recyclerview_row_each_scheme_hits, parent,false);
here you inflating two different layouts so your all TexViews must be present in both layouts . 在这里,您要膨胀两种不同的布局,因此所有TexViews必须同时出现在两种布局中。
OR 要么
place an if condition in onBindViewHolder to handle the null pointer exception because onBindViewHolder must be called in both cases. 在onBindViewHolder中放置一个if条件来处理空指针异常,因为在两种情况下都必须调用onBindViewHolder。
First of all Thank you guys and i really appreciate your help. 首先,谢谢大家,我非常感谢您的帮助。 Actually it was my bad.
其实这是我的坏事。
I was storing the value to success
variable inside my onBindViewHolder()
which is going to be used for inside both method (onCreateViewHolder(), onBindViewHolder() ) and that was my actual mistake 我将
success
变量的值存储在onBindViewHolder()
,这将用于两种方法(onCreateViewHolder(),onBindViewHolder()),这是我的实际错误
because i forgot that i am using this success
variable in my onCreateViewHolder()
as well to select my particular layout for particular situation and because of that mistake when my onCtreatViewHolder()
runs, at that time success=0
and it returns the wrong layout. 因为我忘记了我在
onCreateViewHolder()
中也使用了此success
变量来为特定情况选择我的特定布局,并且因为在我的onCtreatViewHolder()
运行时出现此错误,所以success=0
并返回了错误的布局。
Now the bottom line is this, 现在最重要的是,
I Create the Global variable named as success
我创建名为
success
的全局变量
and inside onCreateViewHolder()
Method i store the value for success
在
onCreateViewHolder()
方法中,我存储success
的值
then, i use this success
variable for my both methods 然后,我将这
success
变量用于两种方法
here is my solution that is working fine currently. 这是我目前可以正常工作的解决方案。
private int success;
// Constructor
public HitsOnSchemesDataDisplayAdapter(Context context){
layoutInflater = LayoutInflater.from(context);
}
public void setSchemesListHitsWise(ArrayList<SchemesHitsPojoModel> schemesListHitsWise){
this.schemesListHitsWise = schemesListHitsWise;
notifyItemRangeChanged(0, schemesListHitsWise.size());
}
onCreateViewHolder() Method onCreateViewHolder()方法
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
MyViewHolder viewHolder;
SchemesHitsPojoModel firstObject = schemesListHitsWise.get(0);
success = firstObject.getSuccess();
switch (success){
case(0):
view = layoutInflater.inflate(R.layout.custom_no_record_found_msg_screen, parent, false);
Log.d("VIEW "," is runned....");
break;
case(1):
view = layoutInflater.inflate(R.layout.custom_recyclerview_row_each_scheme_hits, parent,false);
Log.d("VIEW_2, "," is run....");
break;
default:
view = layoutInflater.inflate(R.layout.custom_no_record_found_msg_screen, parent, false);
break;
}
viewHolder = new MyViewHolder(view);
return viewHolder;
}
onBindViewHolder() Method onBindViewHolder()方法
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
SchemesHitsPojoModel firstObject = schemesListHitsWise.get(0);
//int success = firstObject.getSuccess();
if(success == 0 ){
Log.d("SORRY!, "," We don't have any records to show you.");
}
else if(success == 1){
SchemesHitsPojoModel currentSchemeRow = schemesListHitsWise.get(position);
holder.schemeName.setText(currentSchemeRow.getSchemeName());
int langId = currentSchemeRow.getLanguage_id();
switch(langId){
case(1):
holder.languageName.setText("Punjabi");
break;
case(2):
holder.languageName.setText("English");
}
holder.schemeViewDate.setText(currentSchemeRow.getSchemeViewDate());
holder.schemeHits.setText(String.valueOf(currentSchemeRow.getSchemeHits()));
holder.viewIcon.setImageResource(R.drawable.ic_remove_red_eye_black_18dp);
//holder.totalHitsOnThisScheme.setText(currentSchemeRow.getSchemeTotalHitsCount());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.