問題描述
我在嘗試在對話框中創建 Spinner 時遇到 NullPointerException,并且似乎無法調試它,因為代碼看起來很可靠.想知道其他人是否有任何想法.非常感謝任何幫助.
I'm getting a NullPointerException while attempting to create a Spinner within a dialog and can't seem to debug it because the code looks solid. Wonder if anyone else has any idea. Any help is greatly appreciated.
protected Dialog onCreateDialog(int id)
{
Dialog dialog;
switch(id) {
case DIALOG_SEND_PM:
Spinner spinner = (Spinner)findViewById(R.id.pm_server);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.server_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
dialog = new Dialog(PM.this);
dialog.setContentView(R.layout.send_pm_dialog);
dialog.setTitle(R.string.send_pm);
pmMessage = (EditText) dialog.findViewById(R.id.send_pm_box);
Button sendPm = (Button) dialog.findViewById(R.id.send_pm_button);
sendPm.setOnClickListener(PM.this);
break;
default:
dialog = null;
}
我在 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);我將上下文更改為 MyClass.this 并將異常移至下一行,這讓我感到困惑.我想知道它是否是具有空值的適配器,但我在不在對話框中時以與以前相同的方式調用所有內容.
I get the exception at adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); I changed the context to MyClass.this and the exception moved down to the next line, which confuses me. I'm wondering if it is the adapter having a null value but I call everything the same way I have before while not in a dialog.
相關的 XML 數據:
Relevant XML data:
<LinearLayout>
<TextView/>
<LinearLayout>
<TextView/>
<EditText/>
<TextView/>
<Spinner
android:id="@+id/pm_server"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:background="@drawable/yblueborder"
android:textColor="#ABABAB"/>
</LinearLayout>
<Button/>
</LinearLayout>
編輯了其余的數據,以免占用太多空間.
Edited out the rest of the data so it wouldn't take up too much space.
推薦答案
我已經設法解決了這個問題.這是非常微妙的,我很確定我很幸運.這是工作代碼:
I have managed to fix the issue. It was very subtle and I'm pretty sure I got lucky. Here's the working code:
protected Dialog onCreateDialog(int id)
{
Dialog dialog;
switch(id) {
case DIALOG_SEND_PM:
dialog = new Dialog(PM.this);
dialog.setContentView(R.layout.send_pm_dialog);
dialog.setTitle(R.string.send_pm);
pmMessage = (EditText) dialog.findViewById(R.id.send_pm_box);
Button sendPm = (Button) dialog.findViewById(R.id.send_pm_button);
sendPm.setOnClickListener(PM.this);
Spinner spinner = (Spinner)dialog.findViewById(R.id.pm_server);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.server_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
break;
default:
dialog = null;
}
return dialog;
}
我移動了一些代碼,以便在微調器之前初始化對話框,但這不是問題.我添加了對話框.在 Spinner spinner = (Spinner)dialog.findViewById(R.id.pm_server);這就是訣竅.希望這對其他人有幫助.
I moved some of the code around so that the dialog was initialized before the spinner, but that was not the issue. I added dialog. in Spinner spinner = (Spinner)dialog.findViewById(R.id.pm_server); and that did the trick. Hope this helps others.
這篇關于在自定義對話框中設置微調器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!