我有 MainActivity ,其子活动 PatientActivity 。
PatientActivity 的儿童活动 RecordActivity 。
在 MainActivity 中,我intent
到 PatientActivity 添加额外内容:
MainActivity.java
...
listPatients.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
try {
Intent intent = new Intent(view.getContext(), PatientActivity.class);
intent.putExtra("patient", patients.getJSONObject(position).toString());
startActivity(intent);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
...
在PatientActivity.java
中 ...
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
intent = getIntent();
String patientFromJson = intent.getStringExtra("patient");
patient = Patient.fromJson(patientFromJson);
...
现在,在 PatientActivity 中,我intent
到 RecordActivity 。
在 RecordActivity 中,当我按向上按钮(而不是后退按钮)时,它会崩溃,因为PatientActivity.onCreate()无法找到引用患者,导致NullPointerException。
我该如何解决这个问题?
答案 0 :(得分:1)
试试这个:
Bundle extras = getIntent().getExtras();
ArrayList<String>hi= new ArrayList<String>();
hi = extras.getStringArrayList("patient");
答案 1 :(得分:1)
使用 SharedPreferences 完成@heLLo建议。
PatientActivity.java
...
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
try
{
intent = getIntent();
String patientFromJson = intent.getStringExtra("patient");
patient = Patient.fromJson(patientFromJson);
SharedPreferences.Editor editor = getSharedPreferences("br.com.metadoc", MODE_PRIVATE).edit();
editor.putString("currPatient", patient.toString());
editor.commit();
setTitle(patient.getName());
}
catch(NullPointerException e)
{
String toBeAPatient = getSharedPreferences("br.com.metadoc", MODE_PRIVATE).getString("currPatient", null);
patient = Patient.fromJson(toBeAPatient);
setTitle(patient.getName());
}
...