1

I have an Activity which consists of RecyclerView list.

I declared recycler view as below:

RecyclerView listTickets = (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
listTickets.addItemDecoration(new SimpleDividerItemDecoration(this));
listTickets.setHasFixedSize(true);
listTickets.setLayoutManager(layoutManager);
listTickets.setAdapter(ticketListAdapter);

My adapter (TicketListAdapter):

class TicketListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> 
{
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View itemView;
        switch (viewType){
            case VIEW_TYPE_DATE:
                itemView = inflater.inflate(R.layout.view_holder_tickets_date, parent, false);
                holder = new DateViewHolder(itemView);
                break;
            case VIEW_TYPE_TICKETS:
                itemView = inflater.inflate(R.layout.ticket_row, parent, false);
                holder =  new TicketListViewHolder(itemView);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof TicketListViewHolder){
            ((TicketListViewHolder) holder).onBind(mTicketClassListFilter.get(position), mContext, listener);
        }else if (holder instanceof DateViewHolder){
            ((DateViewHolder) holder).onBind(mTicketClassListFilter.get(position));
        }
    }
}

I am getting the below crash:

08-08 13:38:16.233 19159-19159/com.tcrsoftware.android E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tcrsoftware.android, PID: 19159
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(android.support.v7.widget.RecyclerView$Recycler, android.support.v7.widget.RecyclerView$State, int, int)' on a null object reference
    at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1694)
    at android.view.View.measure(View.java:23279)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
    at android.view.View.measure(View.java:23279)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:23279)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:451)
    at android.view.View.measure(View.java:23279)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at com.android.internal.policy.DecorView.onMeasure(DecorView.java:898)
    at android.view.View.measure(View.java:23279)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2832)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1869)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2124)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1738)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7745)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
    at android.view.Choreographer.doCallbacks(Choreographer.java:723)
    at android.view.Choreographer.doFrame(Choreographer.java:658)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1694)

All are suggesting to set layout manager to recyclervie. but still there is a crash.

Ben P.
  • 52,661
  • 6
  • 95
  • 123
Sai Sunkari
  • 179
  • 3
  • 27

3 Answers3

1

The error is related to your layout manager. Instead, use the following code as per the following order.

 RecyclerView listTickets = (RecyclerView) findViewById(R.id.recyclerView);
 LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
 listTickets.setLayoutManager(layoutManager);
 listTickets.setAdapter(ticketListAdapter);

I have omitted some code so that you can use the important bits first and add the more details after.

Lucem
  • 2,912
  • 3
  • 20
  • 33
  • @sai the log cat points to layout manager but the code looks fine, the easier way is to use a single viewholder in the adapter so you can see where the problem will popup. since the code will be too much on the question, you can host the adapter, viewholder, model and mainactivity on github and i will edit for you. share the link on the question too for others to view – Lucem Aug 08 '18 at 20:58
  • can you check this – Sai Sunkari Aug 08 '18 at 21:12
  • @Lucem your Answer is acceptable (Y), update LayoutManager like this:- LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); OR You can set like this rvLeftMenu.setLayoutManager(new LinearLayoutManager(this)); – Arbaz.in Aug 09 '18 at 06:41
  • Thanks for responses. This and @Yupi solution helped solved this. – Sai Sunkari Aug 10 '18 at 16:34
1

Try to initialize your adapter by following its constructor in your activity. For example:

//Your adapter constructor:
class TicketListAdapter(Context context){
      this.context = context
}

Example of initializing adapter in your activity:

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
listTickets.setLayoutManager(layoutManager);
ticketListAdapter = new TicketListAdapter(this);
listTickets.setAdapter(ticketListAdapter);
1

So after looking into your code I noticed that there was possibility that adapter was set before RecyclerView is connected with its id so consider changing some things inside your code: Make listTickets global variable or field and move all the code directly inside onCreate just leave part listTickets.setAdapter(ticketListAdapter);. So all code above that line cut and paste inside onCreate directly below setContentView just to make sure you are not calling any method for setting adapter before setting the RecyclerView.

Yupi
  • 4,402
  • 3
  • 18
  • 37