Android 对话框调用目标异常

标签 android exception dialog

我想要一个对话框,用户可以在其中输入用户名和密码来注册他的应用程序 显示对话框,但如果我输入任何数据并单击确认按钮,则会出现异常 =( 有人知道为什么我会得到这个异常吗?

    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();
...
...
...

最佳答案

这应该可以使它工作,因为应用程序需要知道它需要从哪个布局(View)中选择带有 id edit_username 的 edittext....因此应该在您创建的 View 上调用 findViewById 方法,而不是在查看当前 Activity ...希望有帮助

EditText oUsername = (EditText) layout.findViewById(R.id.edit_username);
            String strUsername = oUsername.getText().toString(); // This is line where exception is calling
...

关于Android 对话框调用目标异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6371968/

相关文章:

dialog - 如何关闭 flutter 对话框?

安卓对话框 : Removing title bar

java - 启动更新检查程序线程,检查数据库中的新条目

android - 减少(Android)电池时间的是什么?

android - 动态加载数据到 Gridview

java - Hibernate、Spring、@Transactional - 围绕 try/catch?

android - 对话框有圆角但没有透明边缘

java - 如何使 TextView 的最大行数 - 如果更多 - 可滚动

c++ - 如何在异常中抛出有意义的数据?

c++ - 访问内存中不存在的数组索引的正确异常类型?