自定义布局的AlertDialog.Builder无法打开?

时间:2014-02-28 07:23:36

标签: java android layout dynamic spinner

在我的应用程序中,我尝试open a DialogBox two Spinner custom layout,我在arraylist中定义了open dialog box。对于Spinner的项目,我定义了带有下拉项的AlertDialog.Builder rdialog = new AlertDialog.Builder(MainActivity.this); rdialog.setTitle("Flavours"); rdialog.setIcon(android.R.drawable.ic_input_get); LayoutInflater inflater = LayoutInflater.from(getApplicationContext()); alertView = inflater.inflate(R.layout.rptsetting,null); Spinner fSpinner = (Spinner)findViewById(R.id.fSpinner); ArrayList<String> arrayList1 = new ArrayList<String>(); arrayList1.add("Chocolate"); arrayList1.add("vanila"); ArrayAdapter<String> adp = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_spinner_dropdown_item,arrayList1); fSpinner.setAdapter(adp); fSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); Spinner phSpinner = (Spinner)findViewById(R.id.phSpinner); ArrayList<String> arrayList2 = new ArrayList<String>(); arrayList2.add("9991734971"); ArrayAdapter<String> adp1 = new ArrayAdapter<String> (MainActivity.this, android.R.layout.simple_spinner_dropdown_item,arrayList2); phSpinner.setAdapter(adp1); phSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); EditText phNumber = (EditText)findViewById(R.id.phNumber); rdialog.setView(alertView); rdialog.setNeutralButton("SUBMIT", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); AlertDialog rdialog1 = rdialog.create(); rdialog1.show(); ,当选择执行特定功能时。在编译期间,它显示没有错误,但在尝试{{1}}应用程序崩溃时。

代码:

{{1}}

1 个答案:

答案 0 :(得分:4)

你应该替换这个

 Spinner fSpinner = (Spinner)findViewById(R.id.fSpinner);
 Spinner phSpinner = (Spinner)findViewById(R.id.phSpinner);

使用

 Spinner fSpinner = (Spinner)alertView.findViewById(R.id.fSpinner);
 Spinner phSpinner = (Spinner)alertView.findViewById(R.id.phSpinner);

如果EditText来自您的Custom Dialog layout,那么也会更改

EditText phNumber = (EditText)alertView.findViewById(R.id.phNumber);