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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:20:18+00:00 2026-05-17T19:20:18+00:00

I have an Activity in which user can update a specific information clicking in

  • 0

I have an Activity in which user can update a specific information clicking in a button near a label. This buttons than triggers a Dialog where I have some fields to get user input and a button to finish editing.

My problem is that I am not able to get a reference to the button declare in the dialog specific xml layout. The button reference returns null. Follow some code snippet to ilustrate.

Button which fires the event to build the dialog is declared as a instance variable in the activity as follows:

private Button bConfigurarCarro;

than onCreate method:

bConfigurarCarro = (Button)findViewById(R.id.bConfigurarCarro);
 bConfigurarCarro.setOnClickListener(configuraCarroListener);

this correctly fires the event to create the dialog:

protected OnClickListener configuraCarroListener = new OnClickListener(){
public void onClick(View v) {
showDialog(CARRO_DIALOG_ID);
Log.d(TAG, "Executando evento do botão de configuração de carro no abastecimento.");
            }
        };

than to create the dialog Overrides onCreateDialog method like this:

@Override
protected Dialog onCreateDialog(int id) {
  switch (id) {
    case TIME_DIALOG_ID:
        return new TimePickerDialog(this, mTimeSetListener, hora, minuto, false);
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, mDateSetListener, ano, mes, dia);
    case CARRO_DIALOG_ID:
        Log.d(TAG, "Criando dialog de cadastro de carro.");
        dialogCarro = new Dialog(this);
        dialogCarro.setContentView(R.layout.novo_carro_dialog);
        bSalvarCarro = (Button)findViewById(R.id.botaoSalvarCarro);
        bSalvarCarro.setOnClickListener(salvarCarroListener);
        dialogCarro.setTitle("Carro");
        dialogCarro.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        Log.d(TAG, "Dialog de cadastro de carro criado retornando...");
        return dialogCarro;
    }
        return null;
}

The specific line where the NullPointer triggers is the one where after trying to get Button reference above, I than try to setOnClickListener..

bSalvarCarro = (Button)findViewById(R.id.botaoSalvarCarro);
bSalvarCarro.setOnClickListener(salvarCarroListener);

the bSalvarCarro is null.

The xml layout for the Dialog which I try to set above using the line of code:

dialogCarro.setContentView(R.layout.novo_carro_dialog);

Is this one(novo_carro_dialog.xml):

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
            <TableRow>
                <TextView android:id="@+id/marcaCarro"
                    android:layout_width="wrap_content" android:layout_height="wrap_content" 
                    android:text="@string/marcaCarro"/>
                <EditText android:id="@+id/tMarcaCarro" android:singleLine="true" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:width="130px"/>
            </TableRow>
            <TableRow>
                <TextView android:id="@+id/nomeCarro"
                    android:layout_width="wrap_content" android:layout_height="wrap_content" 
                    android:text="@string/nomeCarro"/>
                <EditText android:id="@+id/tNomeCarro" android:singleLine="true" android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>
            </TableRow>
            <TableRow>
                <TextView android:id="@+id/anoCarro"
                    android:layout_width="wrap_content" android:layout_height="wrap_content" 
                    android:text="@string/anoCarro"/>
                <EditText android:id="@+id/tAnoCarro" android:singleLine="true" android:layout_width="fill_parent"
                android:numeric="integer" android:maxLength="4" android:layout_height="wrap_content" />
            </TableRow>
            <TableRow>
                <TextView android:id="@+id/modeloCarro"
                    android:layout_width="wrap_content" android:layout_height="wrap_content" 
                    android:text="@string/modeloCarro"/>
                <EditText android:id="@+id/tModeloCarro" android:singleLine="true" android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
            </TableRow>
            <Button android:id="@+id/botaoSalvarCarro" android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:text="@string/bSalvarCarro" />
</TableLayout>

As you can see the Button is declared with the id botaoSalvarCarro, but trying to get a reference to it returns null. I am a bit confused by this as if I take out the line which sets the listener the dialog is correctly showed, so. How do I get the reference to this button correctly?

  • 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-17T19:20:19+00:00Added an answer on May 17, 2026 at 7:20 pm
    dialogCarro = new Dialog(this);
    dialogCarro.setContentView(R.layout.novo_carro_dialog);
    bSalvarCarro = (Button)dialogCarro.findViewById(R.id.botaoSalvarCarro);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a couple of tables which are used to log user activity for
I have 2 models - User and Activity - which are related by a
I have list of items in my activity which I load dynamically, item after
I have an activity that has a TabHost containing a set of TabSpecs each
i have created a workflow activity that do give the item creater of a
I have a need to do auditing all database activity regardless of whether it
I have a web-based application that notifies users of activity on the site via
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have my own custom Content Provider that loads a database which contains the
I have a question regarding launching new activities. It boils down to this. I

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.