NullpointerException on release build时,我在onResponse中调用一个对象



当我进行发布构建时,它会在某处给出空指针。我已经想通了 当我添加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);
}
}

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

//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 + ''' + 
"}";
}
}

在一个模型类的上方,这个得到数据

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 + ''' + 
"}";
}
}

里面上面一个这个得到医生笔记((

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 的 proguard 规则:

##---------------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

最新更新