2011-11-13 5 views
1

これは私のコードです。私はカスタムダイアログからDatePickerから値を取得したいと思います。私はdp(DatePicker)を初期化しようとするとnullPointerExceptionエラーが発生し、私は変数にDatePickerから値を取ることができません。 :/誰が何が起こっているのか知っていますか? (私はメイン(R.layout.notifications。1つのレイアウトを持っている)と、ダイアログレイアウト(R.layout.notifications_dialog)は日付ピッカーがnotifications_dialogレイアウトである。カスタムダイアログでDatePickerを使用する

public class Notifications extends Activity { 

static final int CUSTOM_DIALOG_ID = 0; 
EditText Notification, Freq; 
Button Save, Cancel; 
int Year, Month , Day ; 
DatePicker dp; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.notifications); 
    Button ok = (Button) findViewById(R.id.ok); 
    dp=(DatePicker)findViewById(R.id.notdate); 


    ok.setOnClickListener(new OnClickListener() {     
      public void onClick(View ovuinfo) { 
       showDialog(CUSTOM_DIALOG_ID);     
       } 
      }); 
} 

@Override 
protected Dialog onCreateDialog(int id) { 
Dialog dialog = null;; 
    switch(id) { 
    case CUSTOM_DIALOG_ID: 
    dialog = new Dialog(Notifications.this); 

    dialog.setContentView(R.layout.notifications_dialog); 
    dialog.setTitle("Add Notification"); 

    Notification = (EditText)dialog.findViewById(R.id.notification); 
    Freq = (EditText)dialog.findViewById(R.id.freq); 
    Save = (Button)dialog.findViewById(R.id.add); 
    Cancel = (Button)dialog.findViewById(R.id.canc); 
    dp=(DatePicker)findViewById(R.id.notdate); 

    final Calendar cal = Calendar.getInstance(); 
    int year = cal.get(Calendar.YEAR); 
    int monthOfYear = cal.get(Calendar.MONTH); 
    int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); 

     // here is the problem! 
     dp.init(year, monthOfYear, dayOfMonth, new OnDateChangedListener() { 
     @Override 
     public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) { 
      Year = year; 
      Month = monthOfYear; 
      Day = dayOfMonth; 
     } 
     }); 

    Save.setOnClickListener(SaveOnClickListener); 
    Cancel.setOnClickListener(CancelOnClickListener); 
     break; 
    } 
    return dialog; 
} 

private Button.OnClickListener SaveOnClickListener = new Button.OnClickListener(){  
     @Override 
     public void onClick(View arg0) { 
      String not = Notification.getText().toString(); 
      String fre = Freq.getText().toString(); 

      Toast toast = Toast.makeText(getApplicationContext(), not+":"+fre+":"+Day+"-"+Month+"-"+Year, Toast.LENGTH_SHORT); 
       toast.show(); 
     }   
    }; 

    private Button.OnClickListener CancelOnClickListener = new Button.OnClickListener(){ 
     @Override 
     public void onClick(View arg0) { 
      dismissDialog(CUSTOM_DIALOG_ID); 
     } 
    }; 

} 

答えて

3

手始めに、あなたはこれを必要としませんあなたのonCreate内の行():

dp=(DatePicker)findViewById(R.id.notdate); 

あなたの日付ピッカービューが画面に表示されていないので、まだこれはあなたのonCreate(内部であなたにnullを返します)。

onCreateDialogインサイド私はあなただけにする必要があると思いますそれ:

dp = (DatePicker)dialog.findViewById(R.id.notdate); 

nullポインタを修正する必要があります。

+0

ああええ!それは正しかった!ダイアログ内の上記宣言は、このようなものでした。どのように愚かな私ですか:P –

関連する問題