gpt4 book ai didi

android - 当我在 onResponse 中调用一个对象时,发布版本出现 NullpointerException

转载 作者:行者123 更新时间:2023-11-29 22:36:18 24 4
gpt4 key购买 nike

当我进行发布构建时,它在某处给出了空指针。我想通了 当我添加 getData.getDoctorNotes() 时,它会崩溃,但不会在调试版本中崩溃。我无法理解什么 是实际问题,但相同的代码在另一个应用程序中运行完美。

   //json
{
"status": true,
"errors": [],
"data": {
"Labs": [],
"Rads": [],
"Pharmacy": [],
"Doctor_Notes": {},
"Referal_Admission": "N",
"Referal_Procedure": "N"
}
}

call.enqueue(new Callback<OpdHistoryResponse>() {
@Override
public void onResponse(Call<OpdHistoryResponse> call, Response<OpdHistoryResponse> response)
{

viewsEnable();
if (response.body() != null) {
OpdHistoryResponse jresponse = response.body();
// when I call getData() it does not crash but when I call getData.getDoctorNotes
it does not crash, this happen only on release build debug build is running
perfectly fine

if (response.body() != null && response.body().isStatus()
&& jresponse != null
&& jresponse.getData().getDoctorNotes() != null &&
jresponse.getData().getDoctorNotes() != null) {
if (!TextUtils.isEmpty(jresponse.getData().getDoctorNotes().getClinicalHistory())
&&
jresponse.getData().getDoctorNotes().getClinicalHistory() != null &&
!jresponse.getData().getDoctorNotes().getClinicalHistory().equals("")) {


tvClinicalHistory.setText(jresponse.getData().getDoctorNotes().getClinicalHistory());
} else {
tvClinicalHistory.setText(R.string.no_data_found);
}

if (!TextUtils.isEmpty(jresponse.getData().getDoctorNotes().getDiagnosis()) &&
jresponse.getData().getDoctorNotes().getDiagnosis() != null &&
!jresponse.getData().getDoctorNotes().getDiagnosis().equals("")) {
tvDiagnosis.setText(jresponse.getData().getDoctorNotes().getDiagnosis());
} else {
tvDiagnosis.setText(R.string.no_data_found);
}

if (!TextUtils.isEmpty(jresponse.getData().getDoctorNotes().getTPlan()) &&
jresponse.getData().getDoctorNotes().getTPlan() != null &&
!jresponse.getData().getDoctorNotes().getTPlan().equals("")) {
tvPlan.setText(jresponse.getData().getDoctorNotes().getTPlan());
} else {
tvPlan.setText(R.string.no_data_found);
}

} else {
dataNotFound();
tvNoDatFound.setText(R.string.no_data_found);
}
}else {
dataNotFound();
tvNoDatFound.setText(R.string.no_data_found);
}
}

这是响应类,其中所有模型都可用并在 fragment 类中使用

//model class
public class OpdHistoryResponse {

@SerializedName("data")
private OpdHistoryDataModel data;

@SerializedName("errors")
private List<Object> errors;

@SerializedName("status")
private boolean status;

public void setData(OpdHistoryDataModel data){
this.data = data;
}

public OpdHistoryDataModel getData(){
return data;
}

public void setErrors(List<Object> errors){
this.errors = errors;
}

public List<Object> getErrors(){
return errors;
}

public void setStatus(boolean status){
this.status = status;
}

public boolean isStatus(){
return status;
}

@Override
public String toString(){
return
"OpdHistoryResponse{" +
"data = '" + data + '\'' +
",errors = '" + errors + '\'' +
",status = '" + status + '\'' +
"}";
}
}

在上面一个模型类里面这个getData

  public class OpdHistoryDataModel {

@SerializedName("Labs")
private List<String> labs;

@SerializedName("Pharmacy")
private List<Object> pharmacy;

@SerializedName("Doctor_Notes")
private OpdDoctorNotesModel doctorNotes;

@SerializedName("Rads")
private List<String> rads;

@SerializedName("Referal_Procedure")
private String referalProcedure;

@SerializedName("Referal_Admission")
private String referalAdmission;

public void setLabs(List<String> labs){
this.labs = labs;
}

public List<String> getLabs(){
return labs;
}

public void setPharmacy(List<Object> pharmacy){
this.pharmacy = pharmacy;
}

public List<Object> getPharmacy(){
return pharmacy;
}

public void setDoctorNotes(OpdDoctorNotesModel doctorNotes){
this.doctorNotes = doctorNotes;
}

//when this object call it gives null pointer in third tab fragment on release build
public OpdDoctorNotesModel getDoctorNotes(){
return doctorNotes;
}

public void setRads(List<String> rads){
this.rads = rads;
}

public List<String> getRads(){
return rads;
}

public void setReferalProcedure(String referalProcedure){
this.referalProcedure = referalProcedure;
}

public String getReferalProcedure(){
return referalProcedure;
}

public void setReferalAdmission(String referalAdmission){
this.referalAdmission = referalAdmission;
}

public String getReferalAdmission(){
return referalAdmission;
}

@Override
public String toString(){
return
"OpdHistoryDataModel{" +
"labs = '" + labs + '\'' +
",pharmacy = '" + pharmacy + '\'' +
",doctor_Notes = '" + doctorNotes + '\'' +
",rads = '" + rads + '\'' +
",referal_Procedure = '" + referalProcedure + '\'' +
",referal_Admission = '" + referalAdmission + '\'' +
"}";
}
}

里面上面这个getDoctorNotes()

     public class OpdDoctorNotesModel {

@SerializedName("t_plan")
private String tPlan;

@SerializedName("clinical_history")
private String clinicalHistory;

@SerializedName("diagnosis")
private String diagnosis;

public void setTPlan(String tPlan){
this.tPlan = tPlan;
}

public String getTPlan(){
return tPlan;
}

public void setClinicalHistory(String clinicalHistory){
this.clinicalHistory = clinicalHistory;
}

public String getClinicalHistory(){
return clinicalHistory;
}

public void setDiagnosis(String diagnosis){
this.diagnosis = diagnosis;
}

public String getDiagnosis(){
return diagnosis;
}

@Override
public String toString(){
return
"OpdDoctorNotesModel{" +
"t_plan = '" + tPlan + '\'' +
",clinical_history = '" + clinicalHistory + '\'' +
",diagnosis = '" + diagnosis + '\'' +
"}";
}
}

最佳答案

由于您使用的是 GSON,并且您只在发布时遇到问题,我假设您没有为 GSON 实现混淆规则:

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { <fields>; }

# Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapter
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}

##---------------End: proguard configuration for Gson ----------

原始文件在这里:https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg

关于android - 当我在 onResponse 中调用一个对象时,发布版本出现 NullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59487141/

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