2010-06-04 33 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);