2010-06-04 34 views
2

显示显示textView和EditView的自定义对话框时。我如何在对话框中访问这些元素。下面是给出错误的代码。android中的自定义提醒对话框中的访问控制

LayoutInflater factory = LayoutInflater.from(this); 
      final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null); 
      return new AlertDialog.Builder(AlertDialogSamples.this) 
       .setIcon(R.drawable.alert_dialog_icon) 
       .setTitle(R.string.alert_dialog_text_entry) 
       .setView(textEntryView) 
       .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 

        string = uetd.getText().toString() + ":" + petd.getText().toString(); /////producing error 
        } 
       }) 
       .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 

         /* User clicked cancel so do some stuff */ 
        } 
       }) 
       .create(); 

public class listdemo extends ListActivity { 
    /** Called when the activity is first created. */ 
    private static final int DIALOG_TEXT_ENTRY = 7; 
    EditText uetd; 
    EditText petd; 
    SharedPreferences.Editor editor; 
    String string; 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES)); 

     ListView lv = getListView(); 
     lv.setTextFilterEnabled(true); 
    uetd=(EditText)findViewById(R.id.username_edit); 
     petd=(EditText)findViewById(R.id.password_edit); 
lv.setOnItemClickListener(new OnItemClickListener() { 


     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 

    showDialog(DIALOG_TEXT_ENTRY); 
} 
     }); 
} 
} 
+1

像往常一样,我们不能看到错误,当你不发布它....你在哪里定义了产生错误的行中使用的变量(即uetd,string和petd ) – RoflcoptrException 2010-06-04 11:38:21

+0

实际上我正在运行模拟器中的代码来关闭错误的应用程序,所以我不知道确切的错误,但我可以说在非UI线程中不允许在语句中访问的元素。我在原始问题中添加了更多代码。 – Maneesh 2010-06-04 11:45:53

+0

在eclipse中切换到ddms透视图,然后切换到logcat窗口。你看到控制台输出 – RoflcoptrException 2010-06-04 11:51:27

回答

2

,而不是与回报新AlertDialog.builder immedately返回对话框此代码后

uetd=(EditText) textEntryView.findViewById(R.id.username_edit); 

.. ..等等....你应该保存一个t的实例他对话框通过创建一个类变量就这样

AlertDialog dialog; 

然后你就可以很容易地在对话框的回调中访问该对话框的观点:

dialog.findViewById(R.id.username_edit); 

你必须这样做,因为在创建时回调它在内存中构建一个新的类,所以当你直接调用findViewById()时,它找不到活动

0

尝试在你的代码下面一行

final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);