开发者

Nullpointerexception in dialog box display: android

开发者 https://www.devze.com 2023-03-31 11:19 出处:网络
I have this code which requires to display the date when clicked on the button mPickDate. I have another class which has the date class. The code of this class is here. But when i run it, it gives a n

I have this code which requires to display the date when clicked on the button mPickDate. I have another class which has the date class. The code of this class is here. But when i run it, it gives a nullpointerexception. Please help!

    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;
    }
}

heres the 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/Androi开发者_C百科dRuntime(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

the full code:

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;
    }
}


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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消