gpt4 book ai didi

java - 如何修复 Java 类中的 'No properties to serialize found on class' 错误?

转载 作者:行者123 更新时间:2023-12-01 19:05:44 28 4
gpt4 key购买 nike

这个问题不是重复的。我尝试过答案中显示的其他方法,但它们似乎不起作用。我试图迭代“用户”集合中的所有文档以获取每个人的用户名,将其存储到 ArrayList 中,并将其显示在我的 ListView 中。

显示的错误是

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.example, PID: 17169
java.lang.RuntimeException: No properties to serialize found on class com.example.example.Model
at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.<init>(com.google.firebase:firebase-firestore@@21.3.1:714)
at com.google.firebase.firestore.util.CustomClassMapper.loadOrCreateBeanMapperForClass(com.google.firebase:firebase-firestore@@21.3.1:377)
at com.google.firebase.firestore.util.CustomClassMapper.convertBean(com.google.firebase:firebase-firestore@@21.3.1:540)
at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-firestore@@21.3.1:253)
at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-firestore@@21.3.1:100)
at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@21.3.1:210)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@21.3.1:116)
at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@21.3.1:188)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@21.3.1:97)
at com.example.example.LeaderboardActivity$1.onSuccess(LeaderboardActivity.java:46)
at com.example.example.LeaderboardActivity$1.onSuccess(LeaderboardActivity.java:42)
at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我的排行榜类(class)

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;


public class LeaderboardActivity extends AppCompatActivity {

ArrayList<String> usersInfo = new ArrayList<>();
FirebaseFirestore fStore = FirebaseFirestore.getInstance();
FirebaseAuth fAuth= FirebaseAuth.getInstance();
String userID= fAuth.getCurrentUser().getUid();

ListView lv;


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

lv = findViewById(R.id.leaderboardlist);
CollectionReference cr = fStore.collection("users");
cr.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots){ //Line 42
Model mod = documentSnapshot.toObject(Model.class);
String Name = mod.getName();
usersInfo.add(Name);
System.out.println(usersInfo); //Line 46
}
}
});
usersInfo.add("aggg");
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,usersInfo);
lv.setAdapter(adapter);
System.out.println(usersInfo);

final Context context = this;

BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {

switch (item.getItemId()) {
case R.id.action_arcade:
Intent intentSettings = new Intent(context, MenuActivity.class);
startActivity(intentSettings);
finish();
break;

case R.id.action_home:
Intent intentHome = new Intent(context, Menu2.class);
startActivity(intentHome);
finish();
break;

case R.id.action_account:
Intent intentAccount = new Intent(context, AccountActivity.class);
startActivity(intentAccount);
finish();
break;
}
return false;
}
});


}

}

我的模型类

import androidx.annotation.Keep;


import java.io.Serializable;@Keep

public class Model implements Serializable {

public static String Name;
public static String Email;
public static int Coin_Toss_High_Score;
public static int Rps_High_Score;
public static int TicTacToe_High_Score;


public Model() {}

public Model(String name, String email, int ticTacToe_High_Score, int coin_Toss_High_Score, int rps_High_Score){
this.Name = name;
this.Email = email;
this.TicTacToe_High_Score = ticTacToe_High_Score;
this.Coin_Toss_High_Score = coin_Toss_High_Score;
this.Rps_High_Score = rps_High_Score;
}

public static String getName() {
return Name;
}

public static String getEmail() {
return Email;
}

public static int getCoin_Toss_High_Score() {
return Coin_Toss_High_Score;
}

public static int getRps_High_Score() {
return Rps_High_Score;
}

public static int getTicTacToe_High_Score() {
return TicTacToe_High_Score;
}

public static void setName(String name) {
Name = name;
}

public static void setEmail(String email) {
Email = email;
}

public static void setCoin_Toss_High_Score(int coin_Toss_High_Score) {
Coin_Toss_High_Score = coin_Toss_High_Score;
}

public static void setRps_High_Score(int rps_High_Score) {
Rps_High_Score = rps_High_Score;
}

public static void setTicTacToe_High_Score(int ticTacToe_High_Score) {
TicTacToe_High_Score = ticTacToe_High_Score;
}
}

最佳答案

您需要添加此Gson

    implementation 'com.squareup.retrofit2:converter-gson:2.6.0'

然后import java.io.Serialized;@Keep删除它并将其导入到您的类中

  import com.google.gson.annotations.SerializedName

关于java - 如何修复 Java 类中的 'No properties to serialize found on class' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59558571/

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