使用Datepicker的Nullpointerexception

时间:2012-02-01 10:55:42

标签: android dialog datepicker

我正试图从对话框上的日期选择器中获取日期。但它正在崩溃与NullpointerExeption,我不知道为什么.. 其他一切工作都很好。所以我只发布了datepicker的相关和编写的代码。

Screenshot of my app

以下Codesnippets:

在公共课中我初步化了datepicker:

public class homework extends ListActivity {
    DatePicker date_pick;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homework);

        //Only some more code for managing database and some functions
    }

    /*** HERE ARE SOME FUNCTIONS AND CODE FOR CREATING THE CONTEXT MENU etc */

    //What to do by clicking the "Hausaufgabe hinzufügen" button
    private Button.OnClickListener add_hw = new Button.OnClickListener(){
    public void onClick(View arg0) {
        mDbHelper.open_database_rw();
        String txt_insert_hw = insert_hw.getText().toString(); 

        if(txt_insert_hw.equals("")){
            doMessage("Keine Eingabe!");
        }else{
            final String INSERT_HW = "INSERT INTO tbl_homework ('hw') VALUES ('=> " + txt_insert_hw + "')";
            db.execSQL(INSERT_HW);
            insert_hw.setText("");

            /***It's crashing here*/
            String date_picker_message= (date_pick.getMonth()+1) +"/"+date_pick.getDayOfMonth()+"/"+date_pick.getYear();
            /****/

            fillData();
        }
    }};

    //Code for dialog
    @Override
    protected Dialog onCreateDialog(int id) {
    Dialog dialog = null;;
    switch(id) {
    case ADD_HW_DIALOG:
     dialog = new Dialog(homework.this);

     dialog.setContentView(R.layout.customlayout_hw);
     dialog.setTitle("Hausaufgabe hinzufügen");

     date_pick= (DatePicker)findViewById(R.id.date_maturity);

     break;
    }
    return dialog;
}
}   

错误代码:

02-01 10:52:33.554: E/AndroidRuntime(230):  Uncaught handler: thread main exiting due to uncaught exception
02-01 10:52:33.574: E/AndroidRuntime(230):  java.lang.NullPointerException
02-01 10:52:33.574: E/AndroidRuntime(230):  at test.marco.notenha.homework$1.onClick(homework.java:175)
02-01 10:52:33.574: E/AndroidRuntime(230):  at android.view.View.performClick(View.java:2364)
02-01 10:52:33.574: E/AndroidRuntime(230):  at android.view.View.onTouchEvent(View.java:4179)
02-01 10:52:33.574: E/AndroidRuntime(230):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
02-01 10:52:33.574: E/AndroidRuntime(230):  at android.view.View.dispatchTouchEvent(View.java:3709)
02-01 10:52:33.574: E/AndroidRuntime(230):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-01 10:52:33.574: E/AndroidRuntime(230):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-01 10:52:33.574: E/AndroidRuntime(230):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-01 10:52:33.574: E/AndroidRuntime(230):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-01 10:52:33.574: E/AndroidRuntime(230):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-01 10:52:33.574: E/AndroidRuntime(230):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-01 10:52:33.574: E/AndroidRuntime(230):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
02-01 10:52:33.574: E/AndroidRuntime(230):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
02-01 10:52:33.574: E/AndroidRuntime(230):  at android.app.Dialog.dispatchTouchEvent(Dialog.java:643)
02-01 10:52:33.574: E/AndroidRuntime(230):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
02-01 10:52:33.574: E/AndroidRuntime(230):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
02-01 10:52:33.574: E/AndroidRuntime(230):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 10:52:33.574: E/AndroidRuntime(230):  at android.os.Looper.loop(Looper.java:123)
02-01 10:52:33.574: E/AndroidRuntime(230):  at android.app.ActivityThread.main(ActivityThread.java:4363)
02-01 10:52:33.574: E/AndroidRuntime(230):  at java.lang.reflect.Method.invokeNative(Native Method)
02-01 10:52:33.574: E/AndroidRuntime(230):  at java.lang.reflect.Method.invoke(Method.java:521)
02-01 10:52:33.574: E/AndroidRuntime(230):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-01 10:52:33.574: E/AndroidRuntime(230):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-01 10:52:33.574: E/AndroidRuntime(230):  at dalvik.system.NativeStart.main(Native Method)
02-01 10:52:33.604: I/dalvikvm(230):        threadid=7: reacting to signal 3
02-01 10:52:33.604: E/dalvikvm(230):        Unable to open stack trace file '/data/anr/traces.txt': Permission denied

2 个答案:

答案 0 :(得分:1)

您的日期选择器似乎是布局R.layout.customlayout_hw的一部分,如果是这样,请更改

date_pick= (DatePicker)findViewById(R.id.date_maturity);

date_pick= (DatePicker)dialog.findViewById(R.id.date_maturity);

答案 1 :(得分:1)

您的date_pick是否从正确的视图中读取?它不在主要活动上,而是在你的对话框上!

date_pick= (DatePicker)dialog.findViewById(R.id.date_maturity);