我想在弹出对话框中创建一个国家微调器,但我的微调器的findViewById无法被识别。这是给我一个错误的行: countryField =(Spinner)dialog.findViewById(R.id.viewSpin);
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DIALOG_TEXT_ENTRY:
// This shows how to add a custom layout to an AlertDialog
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.commentlayout,null);
return new AlertDialog.Builder(HomeActivity.this)
.setIcon(R.drawable.ic_launcher)
.setTitle(R.string.app_name)
.setView(textEntryView)
.setPositiveButton(R.string.Submit,newDialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton) {
//System.out.println("You want to submit");
nameField = (EditText) textEntryView.findViewById(R.id.editText1);
countryField = (Spinner) dialog.findViewById(R.id.viewSpin);// error line
commentField = (EditText) textEntryView.findViewById(R.id.commentField);
//get message from message fields
String name = nameField.getText().toString();
String count = countryField.getSelectedItem().toString();
String comm = commentField.getText().toString();
//check whether the name field is empty or not
if(name.length()>0) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = newHttpPost("URL");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
// nameValuePairs.add(new BasicNameValuePair("id", "01"));
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("country", count));
nameValuePairs.add(new BasicNameValuePair("comment", comm));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
nameField.setText(""); //reset the message text field
//countryField.setText("");
commentField.setText("");
Toast.makeText(getBaseContext(),"Sent",Toast.LENGTH_SHORT).show();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ourBrow.reload();
}
else {
//display message if text field is empty
Toast.makeText(getBaseContext(),"All fields are required",Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
/* User clicked cancel so do some stuff */
System.out.println("You have cancelled");
}
}).create();
}
return null;
}
答案 0 :(得分:0)
它被称为findVIEWbyid是有原因的。它需要一些View的引用,而不是dialogInterface
尝试替换
countryField = (Spinner)dialog.findViewById(R.id.viewSpin);
与
countryField = (Spinner) getView().findViewById(R.id.viewSpin);