問題描述
我有一個活動,用戶可以在其中更新特定信息,點擊標簽附近的按鈕.這個按鈕會觸發一個對話框,其中我有一些字段來獲取用戶輸入和一個按鈕來完成編輯.
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.
我的問題是我無法在對話框特定的 xml 布局中獲得對按鈕聲明的引用.按鈕引用返回 null.按照一些代碼片段來說明.
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;
比onCreate方法:
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.");
}
};
比創建對話框重寫 onCreateDialog 方法是這樣的:
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;
}
NullPointer 觸發的特定行是在嘗試獲取上面的 Button 引用后,我嘗試 setOnClickListener..
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);
bSalvarCarro 為空.
the bSalvarCarro is null.
我嘗試使用代碼行在上面設置的對話框的 xml 布局:
The xml layout for the Dialog which I try to set above using the line of code:
dialogCarro.setContentView(R.layout.novo_carro_dialog);
是這個嗎(novo_carro_dialog.xml):
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>
如您所見,Button 是用 id botaoSalvarCarro 聲明的,但嘗試獲取對它的引用會返回 null.我對此有點困惑,好像我取出設置偵聽器的行,對話框正確顯示,所以.如何正確獲取對該按鈕的引用?
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?
推薦答案
dialogCarro = new Dialog(this);
dialogCarro.setContentView(R.layout.novo_carro_dialog);
bSalvarCarro = (Button)dialogCarro.findViewById(R.id.botaoSalvarCarro);
這篇關于如何使用 xml 布局獲取對在自定義對話框中創建的按鈕的引用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!