我想用微调器显示自定义对话框。奇怪的是,当我尝试设置微调器的适配器时,我收到 NullPointerException...
Dialog dialog = new Dialog(this.getApplicationContext());
dialog.setContentView(R.layout.dialog_spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[] {"0","1","2"});
spin = (Spinner)dialog.findViewById(R.id.spinQ);
//What am I doing wrong here?
spin.setAdapter(spinnerAdapter);
dialog.setTitle("Questions");
dialog.show();
xml布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:paddingLeft="10dip"
>
<Spinner
android:id="@+id/spinQ"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
UPDATE:
AlertDialog alertDialog;
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dialog_spinner,
(ViewGroup) findViewById(R.id.root));
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, new String[] {"0","1","2"});
spin = (Spinner) findViewById(R.id.spinQ);
//I get the error in the following line:
spin.setAdapter(spinnerAdapter);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
Your Spinner
可能还没有充气。如果你想操纵视图,请自己充气,然后使用setContentView
在充气的View
。请参阅docs http://developer.android.com/guide/topics/ui/dialogs.html关于创建对话框。
Update:
在您的新代码中,更改:
spin = (Spinner) findViewById(R.id.spinQ);
to:
spin = (Spinner) layout.findViewById(R.id.spinQ);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)