【问题标题】:How to solve this problems? " Could not deserialize object. "如何解决这些问题? “无法反序列化对象。”
【发布时间】:2022-01-17 21:36:10
【问题描述】:

java.lang.RuntimeException:无法反序列化对象。 com.example.knowledgegain.User 类没有定义无参数构造函数。如果您使用 ProGuard,请确保这些构造函数没有被剥离。

或在这种情况下从 Firestore 获取数据的任何其他方式?

show this exception.

problem in this object.

公共类 WalletFragment 扩展 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();
}

}

【问题讨论】:

    标签: java android firebase google-cloud-platform google-cloud-firestore


    【解决方案1】:

    应该WalletFragment 中添加无参数构造函数。它必须添加到User 类中。为什么?因为在使用以下代码行时,您需要反序列化来自 Firestore 的对象:

    user = documentSnapshot.toObject(User.class);
    

    更多信息,请查看我在以下帖子中的回答:

    【讨论】:

    • 是的,兄弟。我没有在 User 类中添加无参数构造函数。公共用户(){}
    • 感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 2016-12-30
    • 2011-07-23
    相关资源
    最近更新 更多