Wie man Referenz auf eine Schaltfläche erstellt, die in einem benutzerdefinierten Dialogfeld, mit einem xml-layout?

Habe ich eine Aktivität, die Benutzer können ein update bestimmte Informationen Klick auf eine Schaltfläche in der Nähe ein label. Diese Tasten als Auslöser ein Dialogfeld, wo ich einige Felder zu bekommen Benutzer die Eingabe und eine Schaltfläche zum beenden der Bearbeitung.

Mein problem ist, dass ich nicht in der Lage, einen Verweis auf die Schaltfläche, erklären Sie in der dialog-spezifischen xml-layout. Die Schaltfläche Referenz den Wert null zurück. Folgen Sie einige code-snippet zu ilustrate.

Taste, die das Ereignis ausgelöst zu bauen, den dialog erklärt wird, wie eine Instanz-variable in der Aktivität wie folgt:

private Button bConfigurarCarro;

als onCreate-Methode:

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

diese korrekt feuert das Ereignis, um das Dialogfeld erstellen:

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.");
            }
        };

als um das Dialogfeld erstellen Überschreibt onCreateDialog Methode wie diese:

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

Die bestimmte Zeile, wo die NullPointer-Triggern ist die eine, wo nach der Taste "Holen" Referenz oben, die ich, als zu versuchen, setOnClickListener..

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

den bSalvarCarro null ist.

Die xml-layout für den Dialog, die ich versuche zu legen über die Verwendung der code-Zeile:

dialogCarro.setContentView(R.layout.novo_carro_dialog);

Ist dieses(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>

Wie Sie sehen können, ist die Schaltfläche deklariert mit der id botaoSalvarCarro, sondern versuchen, einen Verweis auf null zurück. Ich bin ein bisschen verwirrt von diesem als wenn ich mit der Linie, die die Hörer der dialog ist richtig zeigte, so. Wie bekomme ich den Verweis auf diese Schaltfläche, richtig?

InformationsquelleAutor Marcos Maia | 2010-10-25

Schreibe einen Kommentar