Java 未通过setter保存全局类/变量

Java 未通过setter保存全局类/变量,java,android,global-variables,Java,Android,Global Variables,我正试图使用一个全局类使对象数据在我的所有活动中都可用。在我的第一个活动中,我正在初始化全局类patient,并使用setPatientName设置变量。在我的下一个活动中,我调用“getPatientName”,但它返回null。当我尝试设置“getPatientName”的结果时,它会给出错误信息 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.Char

我正试图使用一个全局类使对象数据在我的所有活动中都可用。在我的第一个活动中,我正在初始化全局类
patient
,并使用
setPatientName
设置变量。在我的下一个活动中,我调用“getPatientName”,但它返回
null
。当我尝试设置“getPatientName”的结果时,它会给出错误信息

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'

编辑:我的第一个活动是从文本字段中收集名称,这就是我试图
setPatientName
as的内容

第一项活动:

 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);
患者类别:

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;
}
manifest.xml:

<application android:name="com.example.imac.chs_pharmacy.Patient"

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

公共类病人扩展应用程序{

//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); 

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

公共类病人扩展应用程序{

//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); 

不需要扩展
应用程序

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); 

不需要扩展
应用程序

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); 


我想问题是你的
tv2
变量是空的。是的,我正在试图弄清楚为什么它是空的。你应该首先检查你是如何检索TextView的,你还没有为此发布代码。啊,我明白了。我没有行
tv2=(TextView)findViewById(R.id.textView2)
但是,它仍然没有在全局变量中设置名称。我想问题是您的
tv2
变量为空。是的,我正在试图弄清楚为什么为空。您应该首先检查如何检索TextView,您还没有为此发布代码。啊,我明白了。我没有行
tv2=(TextView)findViewById(R.id.textView2);
但是,它仍然没有在全局变量中设置名称此代码正在工作!但是我被困在定义“StartPatientName”的部分请参阅已编辑的问题为什么要将患者标记为最终患者?这意味着您不能为患者姓名设置值。@FrankR我刚刚复制了我找到的一些代码。我在两个活动中都关闭了
final
,但结果没有变化您是否尝试注销了在setPatientName方法中设置的值?可能是空字符串或null.这看起来是错误的:`EditText name=(EditText)findViewById(R.id.textView2);`您引用的是同一个TextView?此代码正在工作!但是我被困在定义“StartPatientName”的部分请参阅已编辑的问题为什么要将患者标记为最终患者?这意味着您不能为患者姓名设置值。@FrankR我刚刚复制了我找到的一些代码。我在两个活动中都关闭了
final
,但结果没有变化您是否尝试注销了在setPatientName方法中设置的值?可能是空字符串或n这看起来是错误的:`EditText name=(EditText)findViewById(R.id.textView2)它仍然是一个全局变量吗?我以前从未见过这种格式,使用
instance
它是否仍然需要在AndroidManifest.xml中声明?不需要在AndroidManifest.xml中声明
Patient p=Patient.getInstance();p.setPatientName(namestString);
但它说Patient中的字符串不能应用于0I已更新答案请尝试。更改了
公共静态Patient getInstance()
Patient
classIs中的
方法它仍然是一个全局变量吗?我以前从未见过这种格式,使用
instance
它仍然需要在AndroidManifest.xml中声明吗?不需要在AndroidManifest.xml中声明。它只是另一个java文件。我正在处理它。如何启动实例?我正在尝试g
Patient p=Patient.getInstance();p.setPatientName(namestString);
但它表示Patient中的字符串不能应用于0I已更新答案,请尝试。更改了
Patient
类中的
public static Patient getInstance()
方法