全局类/变量不通过setter保存

时间:2017-01-26 15:31:02

标签: java android global-variables

我正在尝试使用全局类通过我的所有活动使对象数据可用。在我的第一个活动中,我正在初始化全局类JSONObject object = (JSONObject) new JSONTokener(response).nextValue(); finalString = object.getString("text"); callback.onNewData(finalString); ,并使用patient设置变量。在我的下一个活动中,我调用了“getPatientName'”,但它正在返回setPatientName。当我尝试设置' getPatientName'的结果时,它会给我错误

null

编辑:我的第一个活动是从文本字段中收集名称,这就是我尝试java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

的原因

第一项活动:

setPatientName

第二项活动:

 tv2=(TextView)findViewById(R.id.textView2);

 EditText name = (EditText) findViewById(R.id.textView2);
    String nameString = name.getText().toString();

 final Patient p = (Patient) getApplicationContext();
    p.setPatientName(nameString);

患者班:

Patient p = (Patient)getApplication();
String patName = p.getPatientName();
tv2.setText(patName);

的manifest.xml:

package com.example.imac.chs_pharmacy;

import android.app.Application;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class Patient extends Application {

    //private variables
    public String patient_name;

    //default constructor
    public Patient(){
    }

    public Patient(String startPatientName) {
        this.patient_name = startPatientName;
    }

    public void setPatientName( String patientName ){
        Log.d(TAG, "setting patient name");
        this.patient_name = patientName;
    }

    public String getPatientName( ){
        Log.d(TAG, "getting patient name");
        return this.patient_name;
}

还值得注意的是,在我的<application android:name="com.example.imac.chs_pharmacy.Patient" 课程中,我在PatientgetPatientName中注销了一个字符串,但它似乎只是登录setPatientName。我的setPatientName是不是因某种原因被解雇了?

2 个答案:

答案 0 :(得分:1)

让我们试试这个解决方案:

公共类患者延伸申请{

//private variables

public String patient_name = "StartPatientName"; 



 //default constructor    you should not ovveride Application class constructor!
 //    public Patient(){
 //    }

//  public Patient(String startPatientName) {
//    this.patient_name = startPatientName;
//}



public void setPatientName( String patientName ){
    Log.d(TAG, "setting patient name");
    this.patient_name = patientName;

}


public String getPatientName( ){
    Log.d(TAG, "getting patient name");
    return this.patient_name;
}

同样在第二个活动init tv2中如下:

Patient p = (Patient)getApplication();
String patName = p.getPatientName();
TextView tv2 = (TextView) findViewById(R.id.your_text_view_id);
tv2.setText(patName); 

答案 1 :(得分:1)

无需延长Application。请尝试以下

public class Patient {

private static Patient patientInstance;

private String patient_name;

//private contrunctor to prevent from creating patient instance directly through constructor.
private Patient() {
}

public static Patient getInstance() {
  if (patientInstance == null) {
    patientInstance = new Patient();
  }
  return patientInstance;
}

public void setPatientName( String patientName ){
  this.patient_name = patientName;
}

public String getPatientName( ){
  return this.patient_name;
}
}

然后使用下面的课程

Patient p = Patient.getInstance();
String patName = p.getPatientName();
TextView tv2 = (TextView) findViewById(R.id.your_text_view_id);
tv2.setText(patName);