【问题标题】:Android Dialog not recognizing SpinnerAndroid 对话框无法识别 Spinner
【发布时间】:2013-08-16 21:24:00
【问题描述】:

我想在弹出对话框中创建国家/地区微调器,但我的微调器的 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;
       }

【问题讨论】:

  • 不被识别是什么意思?是返回nullpointerexception吗?
  • @gunaseelan 它说:​​方法 findViewById(int) 未定义为 DialogInterface 类型
  • 为什么不使用textEntryView,如下所示。 (Spinner) textEntryView.findViewById(R.id.viewSpin);

标签: android dialog spinner


【解决方案1】:

它被称为 findVIEWbyid 是有原因的。它需要一些对 View 的引用,而不是 dialogInterface

尝试替换

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

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

【讨论】:

  • 新的DialogInterface.OnClickListener()类型的getView()方法未定义{}
  • hmmmm 然后尝试其他一些 get()。可能是 getActivity() 或 getContext()。从远处很难分辨。
  • 问题在于,通常您将小部件连接到 onCreate() 中,您已经拥有正确的 View 参考。现在你必须手动检索它......不知何故。
猜你喜欢
  • 2017-12-27
  • 1970-01-01
  • 1970-01-01
  • 2011-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多