2

私がやりたいことの大まかなアイデア: 日付オブジェクト、いくつかの文字列、およびいくつかのブール値を保持する Events というクラスがあります。DiaryMain.java で startActivityForResult を呼び出したときに返されるデータを使用して作成します。そのオブジェクトを取得して、Diary という ArrayList に入れたいと思います。NullPointer Exception(DiaryMain.java:111)の行diary.add(new Event(date, lengthOfEvent, typeOfEvent, trigger, notes, amb_called, amb_needed, awake));である を取得していDiaryMain.onActivityResult()ます。DiaryMain.java、DiaryEventCreate.java、および Event.java に関連するコードを含めました。データを DiaryMain に正しく返していないのでしょうか、それとも ArrayList が正しく初期化されていませんか?

助けてくれてありがとう。

DiaryMain.java - アプリケーションのメイン アクティビティ

package org.daringer.EpApp;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class DiaryMain extends SherlockFragmentActivity {

public static Context appContext;
public ArrayList<Event> diary;
...

public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
    ...
    case R.id.menu_add_event:{
        makeToast("add event");
        Intent i = new Intent(DiaryMain.this,DiaryEventCreate.class);
        startActivityForResult(i, 0);
        break;
    }
    }
    return true;
}




/* (non-Javadoc)
 * @see android.support.v4.app.FragmentActivity#onActivityResult(int, int, android.content.Intent)
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if(resultCode == RESULT_OK){
        if(requestCode == 0){ //new event [add this event to the end of the diary]
            String notes, lengthOfEvent, typeOfEvent, trigger;
            boolean amb_called, amb_needed, awake; 
            GregorianCalendar date;
            makeToast("CREATED NEW EVENT");
            date = (GregorianCalendar) data.getSerializableExtra("date");
            notes = data.getStringExtra("notes").toString();
            lengthOfEvent = data.getStringExtra("lengthOfEvent").toString();
            typeOfEvent = data.getStringExtra("typeOfEvent").toString();
            trigger = data.getStringExtra("trigger").toString();
            amb_called = data.getBooleanExtra("amb_called", false);
            amb_needed = data.getBooleanExtra("amb_needed", false);
            awake = data.getBooleanExtra("awake", false);
            diary.add(new Event(date, lengthOfEvent, typeOfEvent, trigger, notes, amb_called, amb_needed, awake));

        }
        else if(requestCode == 1){ //edit event [HAVE TO REFERENCE THE EVENTS INDEX]

        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}




}

DiaryEventCreate.java - 入力を受け取って DiaryMain.java に送り返すアクティビティ

package org.daringer.EpApp;

import java.util.GregorianCalendar;

import net.kapati.widgets.DatePicker;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class DiaryEventCreate extends SherlockActivity{

private GregorianCalendar date;
private String lengthOfEvent;
private String typeOfEvent;
private String trigger;
private String notes;
private boolean amb_called;
private boolean amb_needed;
private boolean awake;
private DatePicker ETDate;
private EditText ELength;
private EditText EType;
private EditText ETrigger;
private EditText ENotes;
private CheckBox EAmbCalled;
private CheckBox EAmbNeeded;
private CheckBox EAwake;

private void saveEvent(){

    int   day  = ETDate.getDay();
    int   month= ETDate.getMonth()+1;
    int   year = ETDate.getYear();

    date = new GregorianCalendar(year, month, day);
    makeToast("Saving event...");
    lengthOfEvent = ELength.getText().toString();
    typeOfEvent = EType.getText().toString();
    trigger = ETrigger.getText().toString();
    notes = ENotes.getText().toString();
    amb_called = EAmbCalled.isChecked();
    amb_needed = EAmbNeeded.isChecked();
    awake = EAwake.isChecked();

    if(isInteger(lengthOfEvent)){
        int len = Integer.parseInt(lengthOfEvent);
        if(len>0){
            Intent i = getIntent();
            i.putExtra("lengthOfEvent", lengthOfEvent);
            i.putExtra("date", date);
            i.putExtra("typeOfEvent", typeOfEvent);
            i.putExtra("trigger", trigger);
            i.putExtra("notes", notes);
            i.putExtra("amb_called", amb_called);
            i.putExtra("amb_needed", amb_needed);
            i.putExtra("awake", awake);
            setResult(RESULT_OK, i);
            finish();
        }
        else
            makeToast("Length of event cannot less than 0");
    }
    else{
        makeToast("Length of Event is not an integer");
    }   
}

public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
    ...
    case R.id.event_discard:{
        makeToast("Discarding all entries...");
        finish();
        break;
    }
    case R.id.event_save:{
        saveEvent();
        break;
    }
    }
    return true;
}

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.diary_create_event_menu, menu);
    return super.onCreateOptionsMenu(menu);
}
}

イベント.java

package org.daringer.EpApp;

import java.util.GregorianCalendar;

public class Event{
private GregorianCalendar date;
private String lengthOfEvent;
private String typeOfEvent;
private String trigger;
private String notes;
private boolean amb_called;
private boolean amb_needed;
private boolean awake;

public Event(GregorianCalendar date, String lengthOfEvent, String typeOfEvent, String trigger, String notes, boolean amb_called, boolean amb_needed, boolean awake){
    this.date = date;
    this.lengthOfEvent = lengthOfEvent;
    this.typeOfEvent = typeOfEvent;
    this.trigger = trigger;
    this.notes = notes;
    this.amb_called = amb_called;
    this.amb_needed = amb_needed;
    this.awake = awake;
}

/**
 * @return the amb_called
 */
public boolean isAmb_called() {
    return amb_called;
}

/**
 * @param amb_called the amb_called to set
 */
public void setAmb_called(boolean amb_called) {
    this.amb_called = amb_called;
}

/**
 * @return the amb_needed
 */
public boolean isAmb_needed() {
    return amb_needed;
}

/**
 * @param amb_needed the amb_needed to set
 */
public void setAmb_needed(boolean amb_needed) {
    this.amb_needed = amb_needed;
}

/**
 * @return the awake
 */
public boolean isAwake() {
    return awake;
}

/**
 * @param awake the awake to set
 */
public void setAwake(boolean awake) {
    this.awake = awake;
}

/**
 * @return the date
 */
public GregorianCalendar getDate() {
    return date;
}

/**
 * @param date the date to set
 */
public void setDate(GregorianCalendar date) {
    this.date = date;
}

/**
 * @return the lengthOfEvent
 */
public String getLengthOfEvent() {
    return lengthOfEvent;
}

/**
 * @param lengthOfEvent the lengthOfEvent to set
 */
public void setLengthOfEvent(String lengthOfEvent) {
    this.lengthOfEvent = lengthOfEvent;
}

/**
 * @return the typeOfEvent
 */
public String getTypeOfEvent() {
    return typeOfEvent;
}

/**
 * @param typeOfEvent the typeOfEvent to set
 */
public void setTypeOfEvent(String typeOfEvent) {
    this.typeOfEvent = typeOfEvent;
}

/**
 * @return the trigger
 */
public String getTrigger() {
    return trigger;
}

/**
 * @param trigger the trigger to set
 */
public void setTrigger(String trigger) {
    this.trigger = trigger;
}

/**
 * @return the notes
 */
public String getNotes() {
    return notes;
}

/**
 * @param notes the notes to set
 */
public void setNotes(String notes) {
    this.notes = notes;
}
}

ログキャット

04-24 14:38:45.978: E/AndroidRuntime(6137): FATAL EXCEPTION: main
04-24 14:38:45.978: E/AndroidRuntime(6137): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { cmp=org.daringer.EpApp/.DiaryEventCreate (has extras) }} to activity {org.daringer.EpApp/org.daringer.EpApp.DiaryMain}: java.lang.NullPointerException
04-24 14:38:45.978: E/AndroidRuntime(6137):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
04-24 14:38:45.978: E/AndroidRuntime(6137):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
04-24 14:38:45.978: E/AndroidRuntime(6137):     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
04-24 14:38:45.978: E/AndroidRuntime(6137):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
04-24 14:38:45.978: E/AndroidRuntime(6137):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 14:38:45.978: E/AndroidRuntime(6137):     at android.os.Looper.loop(Looper.java:137)
04-24 14:38:45.978: E/AndroidRuntime(6137):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-24 14:38:45.978: E/AndroidRuntime(6137):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 14:38:45.978: E/AndroidRuntime(6137):     at java.lang.reflect.Method.invoke(Method.java:511)
04-24 14:38:45.978: E/AndroidRuntime(6137):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-24 14:38:45.978: E/AndroidRuntime(6137):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-24 14:38:45.978: E/AndroidRuntime(6137):     at dalvik.system.NativeStart.main(Native Method)
04-24 14:38:45.978: E/AndroidRuntime(6137): Caused by: java.lang.NullPointerException
04-24 14:38:45.978: E/AndroidRuntime(6137):     at org.daringer.EpApp.DiaryMain.onActivityResult(DiaryMain.java:111)
04-24 14:38:45.978: E/AndroidRuntime(6137):     at android.app.Activity.dispatchActivityResult(Activity.java:5293)
04-24 14:38:45.978: E/AndroidRuntime(6137):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
04-24 14:38:45.978: E/AndroidRuntime(6137):     ... 11 more
4

1 に答える 1