简体   繁体   中英

Attempt to invoke virtual method on Attempt to invoke virtual method android.widget.EditText.getText() on a null object reference

Trying to get the value of Edit Text from a EditText inside the Dialog but getting this error again and again

Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

I am inflating a view over another dialog it works fine but whenever I click ok in the dialog I keep getting the above error

LayoutInflater li = LayoutInflater.from(getContext());
                View promptsView = li.inflate(R.layout.custom_dimension_dialog, null);
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        getContext());
                // set prompts.xml to alertdialog builder
                alertDialogBuilder.setView(promptsView);
                alertDialogBuilder
                        .setCancelable(false)
                        .setPositiveButton("OK",
                                new OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        // get user input and set it to result
                                        // edit text
                                        widthEditText = findViewById(R.id.Width);
                                        heightEditText = findViewById(R.id.Height);
                                        String width = widthEditText.getText().toString();
                                        String height = heightEditText.getText().toString();
                                        Toast.makeText(mContext, "Values are: " + width + " " + height, Toast.LENGTH_SHORT).show();
//                                        WIDTH=width
//                                        HEIGHT=height;
                                    }
                                })
                        .setNegativeButton("Cancel",
                                new OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                }).show(); 

You need finding EditText from your dialog view like.

widthEditText = promptsView.findViewById(R.id.Width);
heightEditText = promptsView.findViewById(R.id.Height);

You are not providing any view to the edittext change your code this way

 widthEditText = promptsView.findViewById(R.id.Width);                                 
 heightEditText = promptsView.findViewById(R.id.Height);
widthEditText = promptsView.findViewById(R.id.Width);
heightEditText = promptsView.findViewById(R.id.Height);

you need to use your view in this way promptView.findviewbyId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM