1

ポップアップ ダイアログで国のスピナーを作成したいのですが、スピナーの 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;
       }
4

1 に答える 1

0

理由により、findVIEWbyid と呼ばれます。dialogInterface ではなく、View への参照が必要です。

交換してみてください

countryField = (Spinner)dialog.findViewById(R.id.viewSpin);

countryField = (Spinner) getView().findViewById(R.id.viewSpin);
于 2013-08-16T13:24:00.377 に答える