我是android studio的初学者。
我正在开发一个应用程序来检查构造,我的应用程序是这样构建的: 1 施工场地清单 每个施工现场都有一份控制清单 每个检查点都有严格的检查点
所以我将所有数据保存在 HashMap 中
Hashmap -> 对象 -> HashMap -> 对象 -> 对象 对应于: 施工现场列表 -> 施工现场 -> 控制列表 -> 控制 -> 一种控制
我遇到的问题是保存时出现错误:
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
我不理解,因为我所有的代码都是以相同的方式编写的,当我从保存日期中排除类 ReceptionFerrallage 时,我没有错误。 这是我的代码:
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();
}
}
提前感谢您的回复
我在应用程序中创建一个新的施工站点,然后我查阅列表,当我单击按钮时,我希望获得施工站点列表,但是当我检查列表时,应用程序崩溃了
我不理解,因为我所有的代码都是以相同的方式编写的,当我从保存日期中排除类 ReceptionFerraillage 时,我没有错误。
您的
ReceptionFerraillage
类扩展了 Android 库类,并具有 UI 元素类作为字段。因为 Gson 没有可以处理您的 ReceptionFerraillage
类的内置适配器,所以它使用反射来序列化您的类及其所有超类 包括 Android 库类 的字段。
您应该避免(隐式)依赖 Android 库类的基于反射的序列化,因为您要么依赖于 Android 内部结构,这些内部结构可能会在版本之间发生变化,甚至可能在设备之间有所不同,或者它甚至无法工作,如此处所示。
解决方案可能是拥有一个单独的类,例如
ReceptionFerraillageData
,其中包含要保存和加载的所有数据。例如:
class ReceptionFerraillageData {
String Plan, Cal, Enr, Rec;
String Rem;
}