Android Studio Error : Class declares multiple JSON fields named 'mLifecycleRegistry';

33 views Asked by At

I am beginner in android studio.

I am developping an app to check construction and my app is build like that : 1 List of construction site Eache construction site has a control List Each check point has severel point to check

So i save all my datas in an HashMap<String, Object> and i have a hierarchy to save the data :

Hashmap -> Object -> HashMap -> Object -> Object which correspond to : List of construction Site -> Construction Site - > control List -> Controle -> one kind of controle

The problem i have is when a make the save, i have an error :

java.lang.IllegalArgumentException: Class com.example.suivitravaux.ReceptionFerraillage declares multiple JSON fields named 'mLifecycleRegistry'; conflict is caused by fields androidx.activity.ComponentActivity#mLifecycleRegistry and androidx.core.app.ComponentActivity#mLifecycleRegistry

I don't undersatnd because all my codes are written the same way, and when i exclude the class ReceptionFerraillage from the date to save, i have no error. Here is my code :

    package com.example.suivitravaux;

    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Toast;
    import android.content.Context;
    import androidx.appcompat.app.AppCompatActivity;

    import java.io.Serializable;

    public class ReceptionFerraillage extends AppCompatActivity implements View.OnClickListener, Serializable {
    private RadioGroup plan, cal, enr, rec;

    String Plan, Cal, Enr, Rec;
    String Rem;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reception_ferraillage);

        plan = findViewById(R.id.fer_plan);
        cal = findViewById(R.id.fer_cal);
        enr = findViewById(R.id.fer_enr);
        rec = findViewById(R.id.fer_rec);
        Button btnOk = (Button) findViewById(R.id.ok);
        btnOk.setOnClickListener(this);
    }

    // Méthode pour obtenir un objet ReceptionFerraillage correctement initialisé
    public static ReceptionFerraillage newInstance(Context context) {
        ReceptionFerraillage receptionFerraillage = new ReceptionFerraillage();
        receptionFerraillage.initAttributesFer();
        return receptionFerraillage;
    }

    // Méthode pour initialiser les attributs avec les données de l'interface utilisateur
    private void initAttributesFer() {
        RadioButton radioButtonPlan = findViewById(plan.getCheckedRadioButtonId());
        Plan = radioButtonPlan.getText().toString();

        RadioButton radioButtonCal = findViewById(cal.getCheckedRadioButtonId());
        Cal = radioButtonCal.getText().toString();

        RadioButton radioButtonEnr = findViewById(enr.getCheckedRadioButtonId());
        Enr = radioButtonEnr.getText().toString();

        RadioButton radioButtonRec = findViewById(rec.getCheckedRadioButtonId());
        Rec = radioButtonRec.getText().toString();
    }

    // Méthode appelée lorsqu'on clique sur le bouton "OK"
    @Override
    public void onClick(View v) {
        // Effectuer la validation et mettre à jour les attributs
        initAttributesFer();
        Toast toaster;
        toaster = Toast.makeText(this.getApplicationContext(),Plan,Toast.LENGTH_SHORT);
        toaster.show();
    }
}

Thx in advance for your response

i create a new contruction site in the app and then i consult the list and when i click on the button i expect to have of list of contruction site but when i check the list the app crashes

1

There are 1 answers

0
Marcono1234 On

I don't undersatnd because all my codes are written the same way, and when i exclude the class ReceptionFerraillage from the date to save, i have no error.

Your ReceptionFerraillage class extends an Android library class and has UI element classes as fields. Because Gson has no built-in adapter which can handle your ReceptionFerraillage class, it uses reflection to serialize the fields of your class and all of its superclasses including the Android library classes.

You should avoid relying (implicitly) on reflection-based serialization for Android library classes because you will either depend on Android internals which can change between versions or possibly even differ between devices, or it will not even work, as seen here.

The solution could be to have a separate class, such as ReceptionFerraillageData which contains all the data you want to save and load. For example:

class ReceptionFerraillageData {
    String Plan, Cal, Enr, Rec;
    String Rem;
}