Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7459101

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:07:33+00:00 2026-05-29T14:07:33+00:00

I have a dialog view with 2 buttons, Ok and Cancel. When the user

  • 0

I have a dialog view with 2 buttons, Ok and Cancel. When the user clicks the cancel button I dismiss the dialog using dialog.cancel(). That works fine in the emulator and on my phones. However I am getting some users crash reports showing NullPointerException on the dialog.cancel() line. Does anyone see anything incorrect with the way I’m closing the dialog?

Code:

@Override
public Dialog onCreateDialog(int id) {
    switch (id) {
        case (SHOW_DIALOG): {
            LayoutInflater li1 = LayoutInflater.from(this);
            View wView = li1.inflate(R.layout.dialog_w, null);
            AlertDialog.Builder wDialog = new AlertDialog.Builder(this);
            wDialog.setTitle("Dialog");
            wDialog.setIcon(android.R.drawable.ic_menu_add);
            wDialog.setView(wView);

            final DatePicker wDatePicker = (DatePicker) wView
                                    .findViewById(R.id.dialog_w_Date);
            final TextView DateTitleText = (TextView) wView
                                    .findViewById(R.id.dialog_w_Date_Title);
            final EditText wEditText = (EditText) wView
                                    .findViewById(R.id.dialog_w_w_EditText);
            final EditText bEditText = (EditText) wView
                                    .findViewById(R.id.dialog_w_b_EditText);

            wDialog.setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        String wDateStr = "";
                        String wStr = "";
                        String bStr = "";
                        Calendar wDate = Calendar.getInstance();
                        wDate.set(wDatePicker.getYear(),
                        wDatePicker.getMonth(),
                        wDatePicker.getDayOfMonth());
                        wDateStr = DateUtil.CalendarToString(wDate);
                        wStr = wEditText.getText().toString();
                        bStr = bEditText.getText().toString();
                        if(wStr.length()<1)
                            wStr = Double.toString(userObj.getW());
                        if(bStr.length()<1)
                            bStr = Double.toString(userObj.getB());

                        mDbHelper.insertW(wDateStr,wStr,bStr);

                        updateFromSettings();
                        updateFromDB();
                        updateUI();
                        dialog.cancel();
                        }
                    });

            //NullPointerException Happening here
            wDialog.setNeutralButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                        }
                    });
                    return wDialog.create();
            }
        }
        return null;
    }

@Override
public void onPrepareDialog(int id, Dialog dialog) {
    switch (id) {
        case (SHOW_DIALOG): {
            AlertDialog wDialog = (AlertDialog) dialog;
            DatePicker dp = (DatePicker) wDialog
                                    .findViewById(R.id.dialog_w_Date);
            EditText w = (EditText) wDialog
                                    .findViewById(R.id.dialog_w_w_EditText);
            EditText b = (EditText) wDialog
                                    .findViewById(R.id.dialog_w_b_EditText);

            Calendar c = Calendar.getInstance();
            int cYear = c.get(Calendar.YEAR);
            int cMonth = c.get(Calendar.MONTH);
            int cDay = c.get(Calendar.DAY_OF_MONTH);
            dp.init(cYear, cMonth, cDay, null);
            w.setText("");
            b.setText("");

            w.setHint(Double.toString(userObj.getW()));
            b.setHint(Double.toString(userObj.getB()));

            break;
            }
        }
    }

Here is the StackTrace:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.woodsy.appname/com.woodsy.appname.UserActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4385)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.woodsy.appname.UserActivity.onCreateDialog(UserActivity.java:269)
at android.app.Activity.onCreateDialog(Activity.java:2643)
at android.app.Activity.createDialog(Activity.java:924)
at android.app.Activity.restoreManagedDialogs(Activity.java:913)
at android.app.Activity.performRestoreInstanceState(Activity.java:859)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1138)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1850)
... 11 more
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Related Questions

I have a dialog view with 2 buttons, Ok and Cancel. When the user
I have Webview application that uses progress bar dialog. It works fine but the
I'have a dialog box in JQuery that have two buttons. When the button Upload
I have implemented button's onLongClickListener in Android Dialog which auto-decreases number using timers. Here
I have managed to setup a page so that a user clicks an item
I have a simple list view with some check boxes in an alert dialog.
I have a dialog box that allows users to set hotkeys for use in
I have a dialog that resizes. It also has a custom background which I
I have a dialog that I want to place within another dialog and position
I have a dialog box with controls that is popped up and when a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.