对话框显示中的Nullpointerexception:android

时间:2011-08-26 05:49:13

标签: android dialog nullpointerexception

我有这个代码,需要在点击按钮mPickDate时显示日期。我有另一个具有日期类的类。这个类的代码是here。但是当我运行它时,它会产生一个nullpointerexception。请帮忙!

    mPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);

        }

    });


        super.onCreate(savedInstanceState);
        setContentView(R.layout.offline_screen);
    }
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                        daten.mDateSetListener,
                        daten.mYear, daten.mMonth, daten.mDay);
        }
        return null;
    }
}

继承logcat:

08-26 12:12:57.048: ERROR/AndroidRuntime(889): FATAL EXCEPTION: main
08-26 12:12:57.048: ERROR/AndroidRuntime(889): java.lang.RuntimeException: Unable to start activity ComponentInfo{acb.xiynove/acb.xiynove.ACB_OnlineScreenActivity}: java.lang.NullPointerException
08-26 12:12:57.048: ERROR/AndroidRuntime(889):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
08-26 12:12:57.048: ERROR/AndroidRuntime(889):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
08-26 12:12:57.048: ERROR/AndroidRuntime(889):     at android.app.ActivityThread.access$1500(ActivityThread.java:122)
08-26 12:12:57.048: ERROR/AndroidRuntime(889):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
08-26 12:12:57.048: ERROR/AndroidRuntime(889):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 12:12:57.048: ERROR/AndroidRuntime(889):     at android.os.Looper.loop(Looper.java:132)
08-26 12:12:57.048: ERROR/AndroidRuntime(889):     at android.app.ActivityThread.main(ActivityThread.java:4025)
08-26 12:12:57.048: ERROR/AndroidRuntime(889):     at java.lang.reflect.Method.invokeNative(Native Method)
08-26 12:12:57.048: ERROR/AndroidRuntime(889):     at java.lang.reflect.Method.invoke(Method.java:491)
08-26 12:12:57.048: ERROR/AndroidRuntime(889):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-26 12:12:57.048: ERROR/AndroidRuntime(889):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-26 12:12:57.048: ERROR/AndroidRuntime(889):     at dalvik.system.NativeStart.main(Native Method)
08-26 12:12:57.048: ERROR/AndroidRuntime(889): Caused by: java.lang.NullPointerException
08-26 12:12:57.048: ERROR/AndroidRuntime(889):     at acb.xiynove.ACB_OnlineScreenActivity.onCreate(ACB_OnlineScreenActivity.java:58)
08-26 12:12:57.048: ERROR/AndroidRuntime(889):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-26 12:12:57.048: ERROR/AndroidRuntime(889):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
08-26 12:12:57.048: ERROR/AndroidRuntime(889):     ... 11 more

完整代码:

package acb.ross;
import android.app.Activity;
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import acb.xiynove.*;

public class ACB_OnlineScreenActivity extends Activity {

    HelloDatePickerActivity daten=new HelloDatePickerActivity();
    static final int DATE_DIALOG_ID = 0;    
    Button mPickDate;
    TextView mDateDisplay;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {         


    mPickDate=(Button)findViewById(R.id.pickDate);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.offline_screen);

    mPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);

        }

    });
    daten.currentDate();
    daten.updateDisplay();



    }

    DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                daten.mYear = year;
                daten.mMonth = monthOfYear;
                daten.mDay = dayOfMonth;
                daten.updateDisplay();
            }
        };
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                        mDateSetListener,
                        daten.mYear, daten.mMonth, daten.mDay);
        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:2)

package acb.ross;

import android.app.Activity;
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;


public class ACB_OnlineScreenActivity extends Activity {

    HelloDatePickerActivity daten=new HelloDatePickerActivity();
    static final int DATE_DIALOG_ID = 0;    
    Button mPickDate;
    TextView mDateDisplay;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {         


    super.onCreate(savedInstanceState);
    setContentView(R.layout.offline_screen);
     mDateDisplay=(TextView)findViewById(R.id.dateDisplay);
    mPickDate=(Button)findViewById(R.id.pickDate);

    mPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);

        }

    });
    daten.currentDate();
    daten.updateDisplay();



    }

    DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                daten.mYear = year;
                daten.mMonth = monthOfYear;
                daten.mDay = dayOfMonth;
                daten.updateDisplay();
            }
        };
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                        mDateSetListener,
                        daten.mYear, daten.mMonth, daten.mDay);
        }
        return null;
    }
}