gpt4 book ai didi

java - 无法检索列表 firebase

转载 作者:行者123 更新时间:2023-12-02 12:32:19 26 4
gpt4 key购买 nike

我正在尝试根据我应该从 Firebase 获取的列表构建自定义适配器。

基本上我设置了适配器,然后设置了对匹配节点的引用来检索数据,这里奇怪的是它进入了prepareData函数,但它从未进入监听器,没有成功也没有错误。

我得到了我的适配器的 staktrace:

 FATAL EXCEPTION: main
Process: com.esmad.pdm.friendlymanager, PID: 9887
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at com.esmad.pdm.friendlymanager.other.MatchGameAdapter.getItemCount(MatchGameAdapter.java:62)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3447)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3264)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3798)
at android.view.View.layout(View.java:19393)
at android.view.ViewGroup.layout(ViewGroup.java:6022)
at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1197)
at android.view.View.layout(View.java:19393)
at android.view.ViewGroup.layout(ViewGroup.java:6022)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19393)
at android.view.ViewGroup.layout(ViewGroup.java:6022)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
at android.view.View.layout(View.java:19393)
at android.view.ViewGroup.layout(ViewGroup.java:6022)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19393)
at android.view.ViewGroup.layout(ViewGroup.java:6022)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19393)
at android.view.ViewGroup.layout(ViewGroup.java:6022)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:736)
at android.view.View.layout(View.java:19393)
at android.view.ViewGroup.layout(ViewGroup.java:6022)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2480)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2199)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1385)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6722)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:886)
at android.view.Choreographer.doCallbacks(Choreographer.java:698)
at android.view.Choreographer.doFrame(Choreographer.java:633)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:872)
at android.os.Handler.handleCallback(Handler.java:769)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

这是我的 Activity 代码:

public class AllMatches extends AppCompatActivity implements MatchGameAdapter.OnItemClickListener {

MatchGameAdapter matchGameAdapter;

RecyclerView recyclerView;

ArrayList<MatchViewModel> matches;
FirebaseDatabase database;
HashMap<String, String> matchesKeys;
String eventId;
String eventName;
DatabaseReference myRef;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_matches);

Intent i = getIntent();
Bundle extras = i.getExtras();

database = FirebaseDatabase.getInstance();
if(extras.containsKey("eventId")){
eventId = extras.getString("eventId");
}

if(extras.containsKey("matches")) {
matchesKeys = (HashMap<String, String>) i.getSerializableExtra("matches");
}

if(extras.containsKey("eventName")) {
eventName = extras.getString("eventName");
}

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);

recyclerView = (RecyclerView)findViewById(R.id.rv);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);

matchGameAdapter = new MatchGameAdapter(getApplicationContext(), matches,AllMatches.this);
recyclerView.setAdapter(matchGameAdapter);

prepareData();
}



private void prepareData() {
Log.d("hello","hello");
DatabaseReference myRef = database.getReference("Matches");
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Log.d("hello","hello2");
Match match = snapshot.getValue(Match.class);
MatchViewModel matchViewModel = new MatchViewModel(match.getId(),eventId,eventName,match.getGameEnded(),snapshot.child("date").child("day").getValue().toString(), snapshot.child("date").child("month").getValue().toString(),snapshot.child("date").child("year").getValue().toString(),snapshot.child("date").child("hours").getValue().toString(),snapshot.child("date").child("minutes").getValue().toString());
Iterator it = matchesKeys.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
it.remove(); // avoids a ConcurrentModificationException
if (match.getId().equals(pair.getValue())) {
matches.add(matchViewModel);
}
}
}
matchGameAdapter.notifyDataSetChanged();
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("failed", "here");
}

});
}

@Override
public void onRowClick(String eventId, HashMap<String, Boolean> matches) {

}
}

和我的适配器:

import java.util.ArrayList;
import java.util.HashMap;


public class MatchGameAdapter extends RecyclerView.Adapter<MatchGameAdapter.ViewHolder> {

private OnItemClickListener listener;

public interface OnItemClickListener {
void onRowClick(String eventId, HashMap<String, Boolean> matches);
}

private ArrayList<MatchViewModel> matches;
private Context context;
private View cv;

public MatchGameAdapter(Context context, ArrayList<MatchViewModel> matches, OnItemClickListener listener) {
this.matches = matches;
this.context = context;
this.listener = listener;
}

@Override
public MatchGameAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.eventgame, viewGroup, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final MatchGameAdapter.ViewHolder viewHolder, final int i) {
viewHolder.name.setText(matches.get(i).getEventName() + " " + i + "ª Semana");
viewHolder.city.setText(matches.get(i).getDay() + "/" + matches.get(i).getMonth() + "/" + matches.get(i).getYear() + " " + matches.get(i).getHour() + ":" + matches.get(i).getMinute());
viewHolder.date.setText(matches.get(i).getHasEnded().toString());
viewHolder.field.setImageResource(R.drawable.match);

/*cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null){
listener.onRowClick(events.get(i).getEventId(),events.get(i).getMatches());
}
}
});*/
}

@Override
public int getItemCount() {
return matches.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView field;
private TextView city;
private TextView date;
private TextView name;
public ViewHolder(View view) {
super(view);
cv = view;
name = (TextView)view.findViewById(R.id.eventName);
field = (ImageView)view.findViewById(R.id.field);
city = (TextView)view.findViewById(R.id.city);
date = (TextView)view.findViewById(R.id.date);

}
}

}

最佳答案

只需阅读您的错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at com.esmad.pdm.friendlymanager.other.MatchGameAdapter.getItemCount(MatchGameAdapter.java:62)

getItemCount 函数尝试获取空数组的大小。

您只需使用空数组初始化匹配数组即可。

检查您的数组是否为null

@Override
public int getItemCount() {
if (matches != null) {
return matches.size();
} else {
return 0;
}
}

关于java - 无法检索列表 firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45264173/

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