gpt4 book ai didi

java - 使用 SwipRefresh 库刷新 recyclerview 时 onDatachange 给出错误

转载 作者:行者123 更新时间:2023-12-01 16:17:36 27 4
gpt4 key购买 nike

我正在使用 SwipRefresh 布局刷新我的 recyclerview 但当我添加 recyclerview 方法时在 SwipRefresh 的 onRefresh 内部,我在空对象引用上收到 Datachange 错误,但在不使用 swipreferesh 的情况下,它工作正常,这是我的代码。我尝试了所有可能的方法,但无法解决我的问题,请提供任何建议,谢谢。

  private void Geofireinit() {
started = true;
GeoQuery geoQuery = fire.queryAtLocation(new GeoLocation(20.887715, 77.757623), 50);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {


Log.e("id", key);
Location to = new Location("to");
to.setLatitude(location.latitude);
to.setLongitude(location.longitude);
if (!fetchedUserIds) {
userIdsToLocations.put(key, to);

} else {
userIdsToLocations.put(key, to);
addUserListener(key);
}


}

@Override
public void onKeyExited(String key) {
Log.d("TAG", "onKeyExited: ");
if (userIdsWithListeners.contains(key)) {
int position = getUserPosition(key);
if (position!=-1) {
arrayList.remove(position);
adapter.notifyItemRemoved(position);
}

}
}

@Override
public void onKeyMoved(String key, GeoLocation location) {

}

@Override
public void onGeoQueryReady() {

initialListSize = userIdsToLocations.size();
if (initialListSize == 0) {
fetchedUserIds = true;
}
iterationCount = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
userIdsToLocations.keySet().forEach(this::addUserListener);
}
}

private void addUserListener(String key) {
myref = FirebaseDatabase.getInstance().getReference().child("buses").child(key);
myref.addListenerForSingleValueEvent(userValueListener);
userIdsWithListeners.add(key); // myreference
}

@Override
public void onGeoQueryError(DatabaseError error) {

}
});


geoQueries.add(geoQuery);
}



private void setuplist() {
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new Adapter(this, arrayList);
recyclerView.setAdapter(adapter);
}

@Override
public void onRefresh() {

getrecyclerview(); // getting error when adding this method inside onRefresh
}
private void getrecyclerview() {
swipeRefreshLayout.setRefreshing(true);
userValueListener = new ValueEventListener() {

@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
swipeRefreshLayout.setRefreshing(false);

Mylist mylist = dataSnapshot.getValue(Mylist.class);
mylist.setText(dataSnapshot.child("text").getValue().toString());

if (arrayList.contains(mylist)) {

update(mylist);
} else {

newdata(mylist);
}


}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
};

}

Logcat错误

 java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.firebase.database.ValueEventListener.onDataChange(com.google.firebase.database.DataSnapshot)' on a null object reference

最佳答案

我建议不要使用该库,而应使用 native swiperefreshlayout。像这样的事情:

XML:

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipeRefresh"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvMyRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

然后在 Activity/fragment 中执行以下操作:

swipeRefresh.setOnRefreshListener {

getrecyclerview()
swipeRefresh.isRefreshing = false
}

并且您的 getrecyclerview() 函数不应包含此行 swipeRefreshLayout.setRefreshing(true);

让我知道它是否有效!

关于java - 使用 SwipRefresh 库刷新 recyclerview 时 onDatachange 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62363266/

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