gpt4 book ai didi

android - 尝试在 Activity 中打开时 Recycler View 崩溃

转载 作者:行者123 更新时间:2023-11-29 16:39:09 27 4
gpt4 key购买 nike

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

我声明回收者 View 如下:

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。但仍然有崩溃。

最佳答案

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

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

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

关于android - 尝试在 Activity 中打开时 Recycler View 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51754936/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com