gpt4 book ai didi

java - FirebaseRecyclerAdapter 抛出空对象

转载 作者:行者123 更新时间:2023-12-02 06:13:08 27 4
gpt4 key购买 nike

我的应用程序在登录后立即崩溃,并且在

上给出了一个 null 对象
adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class, R.layout.menu_item, MenuViewHolder.class, category)

有人可以帮忙或者有更好的主意吗?

private void LoadMenu(){
adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class, R.layout.menu_item, MenuViewHolder.class, category) {
@Override
protected void populateViewHolder(MenuViewHolder viewHolder, Category model, int position) {
viewHolder.txtMenuName.setText(model.getName());
Picasso.get().load(model.getImage()).into(viewHolder.imageView);
final Category clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View v, int position, boolean isLongClick) {
//Get CategoryId and send to new Activity
Intent foodList = new Intent(Home.this, FoodList.class);
//Because Category is key, so we just get the key of this item
foodList.putExtra("CategoryId", adapter.getRef(position).getKey());
startActivity(foodList);
}
});
}
};
recycler_menu.setAdapter(adapter);
}

这是导致我的应用程序崩溃的完整堆栈跟踪错误,我不知道为什么,我启动了 FireDatabase 并将数据库引用设置为我的 firedatabase 中的“类别”

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.fahad.foodie, PID: 4287
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.fahad.foodie.Home$2.populateViewHolder(Home.java:88)
at com.fahad.foodie.Home$2.populateViewHolder(Home.java:85)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:184)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1888)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:407)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:693)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6981)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

最佳答案

您的 TextView 未正确定义。

根据 logcat 消息,

viewHolder.txtMenuName 为 NULL。

请在您的 MenuViewHolder 中正确定义它,如下所示,

 public MenuViewHolder(View itemView) {
super(itemView);

txtMenuName = itemView.findViewById(R.id.menu_name);
imageMenu = itemView.findViewById(R.id.menu_image);

itemView.setOnClickListener(this);
}

您必须确保txtMenuName正确映射到中的textview id(例如android:id="@+id/menu_name") menu_item.xml.

关于java - FirebaseRecyclerAdapter 抛出空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55893931/

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