2013-04-11 107 views
1

因此,從菜單項中我想顯示一個允許用戶輸入一些文本的對話框。該對話框工作得很好,但是當我嘗試使用EditText對象執行任何操作時,應用程序關閉。可能與我正在使用的對話框的佈局有關,但我不確定。下面是按相應的菜單項時調用的方法:無法從對話框中的EditText對象獲取文本

private void showChangeDownloadURLDialog() { 
    mapShouldRefresh = false; 

    // Get the layout inflater 
    LayoutInflater inflater = MapsActivity.this.getLayoutInflater(); 

    final EditText mEditText = (EditText) findViewById(R.id.urladdress); 

    new AlertDialog.Builder(MapsActivity.this) 
    .setTitle(R.string.map_menu_test) 
    .setView(inflater.inflate(R.layout.change_url, null)) 
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      Log.i(TAG, "click ok"); 
      String mString = mEditText.getEditableText().toString(); 
      Log.i(TAG, "New URL is: " + mString); 
     } 
    }) 
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // Does nothing 
     } 
    }) 
    .create() 
    .show(); 
} 

任何幫助表示讚賞,謝謝!

+0

拉昇字符串mString = mEditText.getText()來分配View的toString()。 – Richa 2013-04-11 04:15:02

回答

3

正確的代碼是:

LayoutInflater inflater = MapsActivity.this.getLayoutInflater(); 
View content = inflater.inflate(R.layout.change_url, null); 
final EditText mEditText = (EditText) content.findViewById(R.id.urladdress); 
new AlertDialog.Builder(MapsActivity.this) 
    .setTitle(R.string.map_menu_test) 
    .setView(content) 
//.... 
+0

從充氣機獲得的視圖中獲取EditText ...賓果。感謝芽 – Fobos13 2013-04-12 21:44:13

0

試試這個,

private void showChangeDownloadURLDialog() { 
mapShouldRefresh = false; 

// Get the layout inflater 
LayoutInflater inflater = MapsActivity.this.getLayoutInflater(); 

final EditText mEditText = new EditText(MapsActivity.this); 

new AlertDialog.Builder(MapsActivity.this) 
.setTitle(R.string.map_menu_test) 
.setView(mEditText) 
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 

    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     Log.i(TAG, "click ok"); 
     String mString = mEditText.getEditableText().toString(); 
     Log.i(TAG, "New URL is: " + mString); 
    } 
}) 
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     // Does nothing 
    } 
}) 
.create() 
.show(); 

}

0

假設這

final EditText mEditText = (EditText) findViewById(R.id.urladdress); 

R.layout.change_url 

招了下

.setView(inflater.inflate(R.layout.change_url, null)) 

您正在嘗試之前其ViewGrouplayout)充氣