I’m using a Adapter and RecyclerView. I try to change the color of the first and last item in my RecyclerView
But I get this Crashe in play store here my adapter class and main code
my RecyclerView in NestedScrollView I read some tips online and applied them but the crash is not gone
Exception java.lang.NullPointerException:
at com.jodaland.example.TableAdapter.onBindViewHolder (TableAdapter.java:66)
at com.jodaland.example.TableAdapter.onBindViewHolder (TableAdapter.java:17)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder (RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder (RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline (RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline (RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next (LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk (LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill (LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren (LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2 (RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.onMeasure (RecyclerView.java:3540)
at android.view.View.measure (View.java:27131)
at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:7951)
at android.widget.LinearLayout.measureChildBeforeLayout (LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical (LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure (LinearLayout.java:721)
at android.view.View.measure (View.java:27131)
at androidx.core.widget.NestedScrollView.measureChildWithMargins (NestedScrollView.java:1792)
at android.widget.FrameLayout.onMeasure (FrameLayout.java:194)
at androidx.core.widget.NestedScrollView.onMeasure (NestedScrollView.java:627)
at android.view.View.measure (View.java:27131)
at android.widget.LinearLayout.measureVertical (LinearLayout.java:995)
at android.widget.LinearLayout.onMeasure (LinearLayout.java:721)
at android.view.View.measure (View.java:27131)
at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure (ConstraintLayout.java:811)
at androidx.constraintlayout.core.widgets.ConstraintWidgetContainer.measure (ConstraintWidgetContainer.java:632)
at androidx.constraintlayout.core.widgets.ConstraintWidgetContainer.layout (ConstraintWidgetContainer.java:733)
at androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.solveLinearSystem (BasicMeasure.java:160)
at androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.solverMeasure (BasicMeasure.java:291)
at androidx.constraintlayout.core.widgets.ConstraintWidgetContainer.measure (ConstraintWidgetContainer.java:120)
at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem (ConstraintLayout.java:1594)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure (ConstraintLayout.java:1708)
at android.view.View.measure (View.java:27131)
at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:7951)
at android.widget.FrameLayout.onMeasure (FrameLayout.java:194)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure (ContentFrameLayout.java:145)
at android.view.View.measure (View.java:27131)
at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:7951)
at android.widget.LinearLayout.measureChildBeforeLayout (LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical (LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure (LinearLayout.java:721)
at android.view.View.measure (View.java:27131)
at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:7951)
at android.widget.FrameLayout.onMeasure (FrameLayout.java:194)
at android.view.View.measure (View.java:27131)
at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:7951)
at android.widget.LinearLayout.measureChildBeforeLayout (LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical (LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure (LinearLayout.java:721)
at android.view.View.measure (View.java:27131)
at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:7951)
at android.widget.FrameLayout.onMeasure (FrameLayout.java:194)
at com.android.internal.policy.DecorView.onMeasure (DecorView.java:1173)
at android.view.View.measure (View.java:27131)
at android.view.ViewRootImpl.performMeasure (ViewRootImpl.java:4187)
at android.view.ViewRootImpl.measureHierarchy (ViewRootImpl.java:2936)
at android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:3204)
at android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:2618)
at android.view.ViewRootImpl$TraversalRunnable.run (ViewRootImpl.java:9971)
at android.view.Choreographer$CallbackRecord.run (Choreographer.java:1010)
at android.view.Choreographer.doCallbacks (Choreographer.java:809)
at android.view.Choreographer.doFrame (Choreographer.java:744)
at android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:995)
at android.os.Handler.handleCallback (Handler.java:938)
at android.os.Handler.dispatchMessage (Handler.java:99)
at android.os.Looper.loop (Looper.java:246)
at android.app.ActivityThread.main (ActivityThread.java:8653)
at java.lang.reflect.Method.invoke
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)
@Override
public void onBindViewHolder(TableAdapter.ViewHolder holder, int position) {
Modal m = this.modalList.get(position);
if (holder.getItemViewType() == TYPE_HEAD) {
int colorGreen = ContextCompat.getColor(holder.txtTime.getContext(),R.color.Green);
holder.Time.setBackgroundColor(colorGreen);
holder.Time.setText(m.getTime());
int colorGray = ContextCompat.getColor(holder.txtMoney.getContext(),R.color.Gray);
holder.Money.setBackgroundColor(colorGray);
holder.Money.setText(m.getMoney());
int colorOchre = ContextCompat.getColor(holder.txtInterest.getContext(),R.color.Ochre);
holder.profit.setBackgroundColor(colorOchre);
holder.profit.setText(m.getInterest());
int colorBlue = ContextCompat.getColor(holder.txtSum.getContext(),R.color.Blue);
holder.Sum.setBackgroundColor(colorBlue);
holder.Sum.setText(m.getSum());
}else {
//line 66 holder.Time.setText(m.getTime());
holder.Money.setText(m.getMoney());
holder.profit.setText(m.getInterest());
holder.Sum.setText(m.getSum());
}
}
@Override
public int getItemViewType(int position) {
if (position == 0 || position == modalList.size()-1 )
return TYPE_HEAD;
return TYPE_LIST;
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView Time;
public TextView Money;
public TextView profit;
public TextView Sum;
public ViewHolder(@NonNull View itemView) {
super(itemView);
Time = itemView.findViewById(R.id.lbl_time);
Money = itemView.findViewById(R.id.lbl_money);
profit = itemView.findViewById(R.id.lbl_interest);
Sum = itemView.findViewById(R.id.lbl_sum);
}
}
}
Please help and provide some advice
New contributor
fekhreddine bouziane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.