尝试在“活动”中打开时,Recycler视图崩溃

时间:2018-08-08 20:15:08

标签: android listview android-recyclerview recyclerview-layout

我有一个由RecyclerView列表组成的活动。

我声明了以下回收者视图:

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);

我的适配器(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));
        }
    }
}

我遇到以下崩溃:

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)

所有建议将布局管理器设置为recyclervie。但仍然有崩溃。

3 个答案:

答案 0 :(得分:1)

该错误与您的布局管理器有关。相反,请按照以下顺序使用以下代码。

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

我省略了一些代码,以便您可以先使用重要的位,然后再添加更多详细信息。

答案 1 :(得分:1)

因此,在查看您的代码后,我发现有可能在RecyclerViewid连接之前设置了适配器,因此请考虑更改代码中的某些内容: 使listTickets成为全局变量或字段,然后将所有代码直接移到onCreate内,仅保留部分listTickets.setAdapter(ticketListAdapter);。因此,该行上方的所有代码都会在onCreate的正下方setContentView内剪切并粘贴,以确保在设置RecyclerView之前没有调用任何设置适配器的方法。

答案 2 :(得分:0)

尝试通过遵循活动中的适配器构造函数来初始化适配器。 例如:

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

在活动中初始化适配器的示例:

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