Reputation: 568
I am using SwipRefresh layout to refresh my recyclerview but when i am adding the recyclerview method inside the onRefresh of SwipRefresh i am getting error of Datachange on null object reference but without using swipreferesh it is working fine here is my code. I tried all possible things but i could not resolve my problem please any suggestion thankyou.
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 error
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
Upvotes: 0
Views: 96
Reputation: 585
I would recommend not using the library but the native swiperefreshlayout. Something like this:
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>
Then in activity/fragment you do this:
swipeRefresh.setOnRefreshListener {
getrecyclerview()
swipeRefresh.isRefreshing = false
}
And your getrecyclerview() function should not have this line swipeRefreshLayout.setRefreshing(true);
let me know if it works!
Upvotes: 1