1

因此,我想从一个菜单项中显示一个允许用户输入一些文本的对话框。该对话框工作得很好,但是当我尝试对 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();
}

任何帮助表示赞赏,谢谢!

4

3 回答 3

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)
//....
于 2013-04-11T04:16:04.160 回答
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();

}

于 2013-04-11T04:18:47.967 回答
0

假设这个

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

R.layout.change_url

把它移到下面

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

您正在尝试View在其ViewGroup( layout) 膨胀之前分配

于 2013-04-11T04:20:54.313 回答