添加输入类型时 EditText 崩溃

发布于 2025-01-07 05:20:50 字数 1029 浏览 1 评论 0原文

在我的 Android 应用程序中,我有一个对话框,用户在其中将信息输入到 EditText 并保存数据。到目前为止,一切工作正常,直到我将 inputType 添加到 EditTexts 中。我似乎找不到这个问题的解决方案,我对 android 编程和一般编程相当陌生,所以这可能是一个愚蠢的错误,但我无法弄清楚。这里是一些代码:

private Dialog dialog() {
    Dialog diUnit = new Dialog(Overzicht.this);
    diUnit.setContentView(R.layout.unitdialog);
    EditText etKM = (EditText) diUnit.findViewById(R.id.etKM);
    etKM.setInputType(InputType.TYPE_CLASS_NUMBER);
    diUnit.setTitle("Add unit");
    diUnit.setCancelable(false);
    diUnit.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;
    bUnitDialogSave = (Button) diUnit.findViewById(R.id.bUnitDialogVoegToe);
    bUnitDialogCancel = (Button) diUnit.findViewById(R.id.bUnitDialogCancel);
    bUnitDialogCancel.setOnClickListener(this);
    bUnitDialogAdd.setOnClickListener(this);
    return diUnit;
}

和 logcat :

image 1

我知道它没有存储 EditText尚未输入,但一旦我添加 setInputType 行,问题就开始了。

In my android application I have a dialog box in which the user inputs info into an EditText and saves the data. Everything so far worked fine until I added an inputType to the EditTexts. I can't seem to find the solution to this problem, I'm rather new to android programming and programming in general so it might be a dumb mistake but I can't figure it out. Here some of the code:

private Dialog dialog() {
    Dialog diUnit = new Dialog(Overzicht.this);
    diUnit.setContentView(R.layout.unitdialog);
    EditText etKM = (EditText) diUnit.findViewById(R.id.etKM);
    etKM.setInputType(InputType.TYPE_CLASS_NUMBER);
    diUnit.setTitle("Add unit");
    diUnit.setCancelable(false);
    diUnit.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;
    bUnitDialogSave = (Button) diUnit.findViewById(R.id.bUnitDialogVoegToe);
    bUnitDialogCancel = (Button) diUnit.findViewById(R.id.bUnitDialogCancel);
    bUnitDialogCancel.setOnClickListener(this);
    bUnitDialogAdd.setOnClickListener(this);
    return diUnit;
}

And the logcat :

image 1

I know it's not storing the EditText input yet but the problem starts as soon as I add the setInputType line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

财迷小姐 2025-01-14 05:20:50

尝试将布局 R.layout.unitdialog 膨胀为 View(使用 LayoutInflater),然后搜索该 EditText 在膨胀的 View 中:

private Dialog dialog() {
    Dialog diUnit = new Dialog(Overzicht.this);
    LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
    View content = inflater.inflate(R.layout.unitdialog, null);
    diUnit.setContentView(content);
    EditText etKM = (EditText) content.findViewById(R.id.etKM);
    etKM.setInputType(InputType.TYPE_CLASS_NUMBER);
    diUnit.setTitle("Add unit");
    diUnit.setCancelable(false);
    diUnit.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;
    bUnitDialogSave = (Button) diUnit.findViewById(R.id.bUnitDialogVoegToe);
    bUnitDialogCancel = (Button) diUnit.findViewById(R.id.bUnitDialogCancel);
    bUnitDialogCancel.setOnClickListener(this);
    bUnitDialogAdd.setOnClickListener(this);
    return diUnit;

Try to inflate the layout R.layout.unitdialog into a View(with the LayoutInflater) and then search for that EditText in the inflated View:

private Dialog dialog() {
    Dialog diUnit = new Dialog(Overzicht.this);
    LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
    View content = inflater.inflate(R.layout.unitdialog, null);
    diUnit.setContentView(content);
    EditText etKM = (EditText) content.findViewById(R.id.etKM);
    etKM.setInputType(InputType.TYPE_CLASS_NUMBER);
    diUnit.setTitle("Add unit");
    diUnit.setCancelable(false);
    diUnit.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;
    bUnitDialogSave = (Button) diUnit.findViewById(R.id.bUnitDialogVoegToe);
    bUnitDialogCancel = (Button) diUnit.findViewById(R.id.bUnitDialogCancel);
    bUnitDialogCancel.setOnClickListener(this);
    bUnitDialogAdd.setOnClickListener(this);
    return diUnit;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文