2011-11-13 3 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 라인() : 당신의 DatePicker에서보기가 화면에 아니기 때문에

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

아직이 당신의에서 onCreate() 안에 당신에게 null를 돌려 주어,이 onCreateDialog 내부

나는 그냥 만들 필요가 있다고 생각 그것 :

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

null 포인터가 수정되어야합니다.

+0

오 그래! 맞았 어! 대화 상자 안의 위의 선언은 다음과 같습니다. 얼마나 어리석은가 : P –

관련 문제