开发者

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 lay开发者_StackOverflowout = 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();
...
...
...


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
...


If this code is taking place in an Activity, then findViewById(...) is probably not going to work the way you are expecting. Do you have an EditText view in your activity's layout?

Instead try this:

EditText oUsername = (EditText) layout.findViewById(R.id.edit_username);
String strUsername = oUsername.getText().toString();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜