java.lang.RuntimeException: Could not deserialize object. Class com.example.knowledgegain.User does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
or any other way of getting data from Firestore in this situation?
public class WalletFragment extends Fragment {
public WalletFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
FragmentWalletBinding binding;
FirebaseFirestore database;
User user;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentWalletBinding.inflate(inflater,container,false);
database = FirebaseFirestore.getInstance();
try {
database.collection("User").document(FirebaseAuth.getInstance().getUid())
.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
try {
user = documentSnapshot.toObject(User.class);
binding.showCoinsId.setText(String.valueOf(user.getCoins()));
}catch (Exception e){
Log.e("TAG", "onSuccess: ", e );
}
}
});
}catch (Exception e){
Log.d("TAg", e.toString());
Toast.makeText(getContext(),e.toString(),Toast.LENGTH_LONG).show();
}
return binding.getRoot();
}
}
CodePudding user response:
The no-argument constructor should not be added in the WalletFragment
. It must be added in the User
class. Why? Because you need to deserialize the objects that are coming from the Firestore when using the following line of code:
user = documentSnapshot.toObject(User.class);
For more information, please check my answer from the following post: