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

  • Home
  • SEARCH
  • 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 8579537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:37:42+00:00 2026-06-11T20:37:42+00:00

I have updated the alert dialog box format using my own layout xml. In

  • 0

I have updated the alert dialog box format using my own layout xml.

In this customerized alert dialog box there are 2 buttons, one button is to save the data being input, another one is a cancel button.

How could I write for the CANCEL button such that when the user click it, just simply to DISMISS the dialog box?

   public OnClickListener NewRowButtonListener = new OnClickListener()
   {
      @Override
      public void onClick(View v) 
      {               
          AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
          builder.setView(getLayoutInflater().inflate(R.layout.custom_dialog_add, null));
          builder.create();

          AlertDialog Custom_dialog_add = builder.create();
          Custom_dialog_add.show(); // show the Dialog

          Button CancelButton = (Button) findViewById(R.id.CancelButton);
          CancelButton.setOnClickListener(new View.OnClickListener() {       
              @Override 
              public void onClick(View v) {Custom_dialog_add.cancel();}  //WRONG: Cannot refer to a non-final variable Custom_dialog_add inside an inner class defined in a different method
            });         
      } 
   };  

it is now revised as follows:

       public OnClickListener NewRowButtonListener = new OnClickListener() 
       { 
          @Override 
          public void onClick(View v)  
          { 
              AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); 
              dialog.setView(getLayoutInflater().inflate(R.layout.custom_dialog_add, null)); 
              dialog.create(); 

               final AlertDialog test = dialog.create(); 
               test.show();

               Button close = (Button) findViewById(R.id.CancelButton); 
               close.setOnClickListener(new android.view.View.OnClickListener() { 
                   public void onClick(View v) { 
                       test.dismiss(); 
                   } 
               });
          }
       };

The Eclipse does not report bugs to the revised codings, but when simulated, it runs out a nullpointer exception. The logcat is as follows. How could such be solved?

09-28 20:15:19.505: E/AndroidRuntime(25847): FATAL EXCEPTION: main
09-28 20:15:19.505: E/AndroidRuntime(25847): java.lang.NullPointerException
09-28 20:15:19.505: E/AndroidRuntime(25847):    at com.pearappx.gamescore3.MainActivity$4.onClick(MainActivity.java:422)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at android.view.View.performClick(View.java:3627)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at android.view.View$PerformClick.run(View.java:14329)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at android.os.Handler.handleCallback(Handler.java:605)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at android.os.Looper.loop(Looper.java:137)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at android.app.ActivityThread.main(ActivityThread.java:4511)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at java.lang.reflect.Method.invokeNative(Native Method)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at java.lang.reflect.Method.invoke(Method.java:511)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
09-28 20:15:19.505: E/AndroidRuntime(25847):    at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 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

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:37:44+00:00Added an answer on June 11, 2026 at 8:37 pm

    Would this code snippet help you?

            LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.custom_layout, null);
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("dialog");
            dialog.setView(view);
            final AlertDialog test = dialog.create();
    
    
            Button close = (Button) view.findViewById(R.id.close_button);
            close.setOnClickListener(new android.view.View.OnClickListener() {
                public void onClick(View v) {
                    test.dismiss();
    
                }
            });
    

    Edit updated version:

        //Create new alert dialog
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        //set title
        dialog.setTitle("title");
        //create the dialog in a final context
        final AlertDialog test = dialog.create();
        //inflate the custom layout in to a View object
        View view = getLayoutInflater().inflate(R.layout.custom_dialog_add, null);
    
        //find the Button object within the inflated view
        //                       ↓↓↓
        Button close = (Button) view.findViewById(R.id.CancelButton);
        //set the onClickListener
        close.setOnClickListener(new OnClickListener() { 
            public void onClick(View v) { 
                test.dismiss(); 
            } 
        });
        //show the dialog
        test.show();
    

    Don’t forget to use the right imports!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an alert box that displays updates processed in php to the user
I have updated my jSon object using the code <script type=text/javascript > jQuery(document).ready(function($){ var
I have updated my symfony version to 2.0.12 version. But I have this error
I have updated my website. Previously there were links like these: http://example.com/bla-bla-bla?language=de . After
I have JQuery dialog box for inserting new row in table. And everything works
I have a gridview in an update panel and am using a jQuery dialog
I have an Activity whose layout is in the file main.xml that contains a
I have a simple YUI dialog with 2 buttons - Accept and Decline. I
I have an intent that starts an activity that shows an alert dialog when
var dlg = $(#dialog).dialog({ autoOpen: false, modal: true, buttons: { 'Update': function() { alert(clientCode);

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.