Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Dialog InvocationTargetException

I want a dialog where the user can enter a Username and a Password to register his application the dialog is shown up but if i enter any data and click on my confirm button the exception is coming up =( anyone know why i get this exception ?

    Context mContext = getApplicationContext();
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.dialog_register_app, (ViewGroup) findViewById(R.id.layout_root));

    //Start building dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(getString(R.string.dialog_register_app_title));
    builder.setView(layout);
    builder.setCancelable(false);
    builder.setPositiveButton(getString(R.string.dialog_register_confirm_button_text), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            //Auslesen der EditText
            EditText oUsername = (EditText) findViewById(R.id.edit_username);
            String strUsername = oUsername.getText().toString(); // This is line where exception is calling
            //Do something with strUsername
        }
   });
   AlertDialog alert = builder.create();
   alert.show();
...
...
...
like image 528
Xetoxyc Avatar asked Jan 23 '26 14:01

Xetoxyc


1 Answers

This should make it work , as the app needs to know from which layout(View ) it needs to pick the edittext with id edit_username....so the findViewById method should be called on the view you have created and not the view of the current activity...hope it helps

EditText oUsername = (EditText) layout.findViewById(R.id.edit_username);
            String strUsername = oUsername.getText().toString(); // This is line where exception is calling
...
like image 69
Nitin Avatar answered Jan 26 '26 05:01

Nitin