次の問題を解決するには、少し助けが必要です。
A と B の 2 つのアクティビティがあります。アクティビティ A は結果のために B を開始し、B はシリアライズ可能なオブジェクトを A に送り返します。
アクティビティ A 開始アクティビティ B:
...
Intent intent = new Intent(MainActivity.this, ExpenseActivity.class);
startActivityForResult(intent, EXPENSE_RESULT);
...
A にデータを送信するアクティビティ B:
...
ExpensePacket packet = new ExpensePacket();
...
Intent returnIntent = new Intent();
returnIntent.putExtra(MainActivity.PACKET_INTENT,packet);
setResult(RESULT_OK,returnIntent);
finish();
アクティビティ A が B から送信されたデータを取得するポイント:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == EXPENSE_RESULT) {
if(resultCode == RESULT_OK){
ExpensePacket result = (ExpensePacket)getIntent().getSerializableExtra(PACKET_INTENT);
wallet.Add(result);
PopulateList();
}
else{
//Write your code otherwise
}
}
}
B から A に送信される私の Serializable オブジェクト:
class ExpensePacket implements Serializable {
private static final long serialVersionUID = 1L;
private Calendar calendar;
public void SetCalendar(Calendar aCalendar){
calendar = aCalendar;
}
public Calendar GetCalendar(){
return calendar;
}
public String GetMonthYear() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
sdf.format(calendar.getTime());
String returnString = "";
returnString += sdf.toString();
returnString += "/";
returnString += Functions.GetMonthName(calendar);
return sdf.toString();
}
}
B から A に送信されたデータが null ポインターである理由を理解するのを手伝ってくれる人がいます。