Get editText value from alertDialog builder

谁说我不能喝 提交于 2019-12-11 10:53:40

问题


I'm new to android. I need to get editText's value to search something. But when I run the program, error comes as null pointer exception.

"EditText etSearch" isn't getting the text of it.

Please help me. Thanks.

public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_search) {
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    MainActivity.this);
            // Get the layout inflater
            LayoutInflater inflater = MainActivity.this.getLayoutInflater();

            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog
            // layout
            builder.setView(inflater.inflate(R.layout.dialog_save, null))

                    // Add action buttons
                    .setPositiveButton("Search",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    final EditText etSearch = (EditText) findViewById(R.id.etSearchText);
                                    mSearchText = etSearch.getText().toString();

                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    dialog.cancel();
                                }
                            });
            // return builder.create();
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

回答1:


For find view from Diloag then you have to give refference of that view of Dialog.

 LayoutInflater inflater = MainActivity.this.getLayoutInflater();
            View mView = inflater.inflate(R.layout.dialog_save, null);
            final EditText etSearch = (EditText)mView.findViewById(R.id.etSearchText);
            builder.setView(mView)

Full Code :

public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_search) {
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    MainActivity.this);
            // Get the layout inflater
            LayoutInflater inflater = MainActivity.this.getLayoutInflater();
            View mView = inflater.inflate(R.layout.dialog_save, null);
            final EditText etSearch = (EditText)mView.findViewById(R.id.etSearchText);
            builder.setView(mView)

                    // Add action buttons
                    .setPositiveButton("Search",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int id) {

                                    mSearchText = etSearch.getText().toString();

                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    dialog.cancel();
                                }
                            });
            // return builder.create();
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }



回答2:


Replace this code:

final EditText etSearch = (EditText) findViewById(R.id.etSearchText);

with

final EditText etSearch = (EditText)dialog. findViewById(R.id.etSearchText);



回答3:


View v= inflater.inflate(R.layout.dialog_save, null); builder.setView(v)

                // Add action buttons
                .setPositiveButton("Search",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                final EditText etSearch = (EditText) v.findViewById(R.id.etSearchText);
                                mSearchText = etSearch.getText().toString();

                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                            }
                        });


来源:https://stackoverflow.com/questions/31153018/get-edittext-value-from-alertdialog-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!