简体   繁体   中英

How to fix 'No properties to serialize found on class' error in a Java class?

This question is not a duplicate. I have tried other methods shown in answers, but they don't seem to work. I am trying to iterate through all the documents in my "users" collection to get everyone's user name, store it into an ArrayList, and display it in my listview.

My ERROR Shown Is

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)

My Leaderboard 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;
        }
    });


}

}

My Model Class

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;
}
}

You need to Add this Gson library

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

then import java.io.Serializable;@Keep remove this and import this in your class

  import com.google.gson.annotations.SerializedName

change this :

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;

To this :

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

Even i thing if you already have getter() setter() method this way even you are making it public you can access all this property through getter() and setter() method.

This should be like this:

public class Model implements Serializable {

    private String Name;
    private String Email;
    private int Coin_Toss_High_Score;
    private int Rps_High_Score;
    private int TicTacToe_High_Score;

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

    public String getName() {
        return Name;
    }

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

    public String getEmail() {
        return Email;
    }

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

    public int getCoin_Toss_High_Score() {
        return Coin_Toss_High_Score;
    }

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

    public int getRps_High_Score() {
        return Rps_High_Score;
    }

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

    public int getTicTacToe_High_Score() {
        return TicTacToe_High_Score;
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM