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 5989755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:08:49+00:00 2026-05-22T23:08:49+00:00

I am new to Android development. Please excuse me If my question is very

  • 0

I am new to Android development. Please excuse me If my question is very simple.

I have tried to create a button on my Android Layout view using XML. Now within the Activity class I am trying to get the button and add a on click listner to it. This is working fine without any issues.

On similar lines on the button click i explained previously I have a dialog being popped up. In this dialog I have a ImageButton. On click of this Image button I am trying to set a on click listner using the below code.

 The Activity on create is as below

@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final Button button = (Button) findViewById(R.id.btnAdd);        
    button.setOnClickListener(this);

}

 @Override
public void onClick(View v) {
final Button btnAdd = (Button) findViewById(R.id.btnAdd);
         if(v==btnAdd) {
            dialog = new Dialog(this);
            dialog.setContentView(R.layout.add_dialog);
            dialog.setTitle("Test Title.");
            dialog.setCancelable(true);

            dialog.show();

        final ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
        try {
            Log.i("Log","1");
            button.setOnClickListener(this);
            Log.i("Log","2");

        }
        catch(Exception e)
        {
            Log.i("Log","3");
            dialog.dismiss();
            //Dialog d = new Dialog(this);
            //d.setTitle("test.");
            Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
            Toast.makeText(this,e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
            Toast.makeText(this,e.toString(),Toast.LENGTH_LONG).show();
            Log.i("Log","4");
            //d.show();
            Log.i("Log","5");

        }
     }

}

In the above I get the Log in this sequence. 1,3,4,5. I dont get the 2. In the toast i get message of blank, blank followed by java.lang.Nullexception.

But this when executed gives me a force close pop up. Please advice how to do this. Or is there any workaround for the same? I need to have a dialog box to come on a button click, and then within the dialog I need to have more than one option of buttons. For each of buttons in the dialog I need to perform different activities. Any kind of help or advice is appreciable. Thank you in advance for your time and help.

  • 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-05-22T23:08:49+00:00Added an answer on May 22, 2026 at 11:08 pm

    Most probably you are trying to retrieve the button from the Activity class. It returns null because this method will only retrieve resources attached to the Activity (by using the method setContentView).

    You have two options:

    • You can inflate the dialog layout using a LayoutInflater
    • If you are extending the Dialog class, add the listener inside that class instead.

    Edit after the update:

    As I said above, the problem is:

       final ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
    

    because imageButton1 is not part of the layout in the activity. Just replace it by:

       final ImageButton button = (ImageButton) dialog.findViewById(R.id.imageButton1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a new web app that is packaged as a WAR as part
I have found this example on StackOverflow: var people = new List<Person> { new
IE is giving me an undefined NAN when i try to view the calender...
I have a project that adds elements to an AutoCad drawing. I noticed that
I have a script that appends some rows to a table. One of the
I am attempting to pull some information from my tnsnames file using regex. I
I would like to update my SQL lite database with the native update-method of
I want to use a temp directory that will be unique to this build.
I'm trying to build a Chrome browser extension, that should enhance the way the
I'm getting an error here that says I haven't defined a method, but it

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.