2013-08-16 3 views
1

Я хочу создать счетчик стран в всплывающем диалоговом окне, но findViewById для моего счетчика не распознается. это строка, дающая мне ошибку: countryField = (Spinner) dialog.findViewById (R.id.viewSpin);Android Dialog не распознает Spinner

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

не признается, что означает? Возвращает 'nullpointerexception'? – Gunaseelan

+0

@gunaseelan это говорит: Метод findViewById (int) не определен для типа DialogInterface – blessed

+0

Почему вы не используете 'textEntryView', как показано ниже. '(Spinner) textEntryView.findViewById (R.id.viewSpin);' – Gunaseelan

ответ

0

это называется findVIEWbyid по причине. она нуждается в некоторой ссылки на View, а не dialogInterface

попробовать заменить

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

с

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

, когда я использую getView(), он показывает ошибку – blessed

+0

AN error !? действительно ли мне нужно вытащить его из вашего носа, КОТОРУЮ вы получили ошибку ... – bofredo

+0

Метод getView() не определен для типа new DialogInterface.OnClickListener() {} – blessed

Смежные вопросы