android - 添加输入类型时 EditText 崩溃

标签 android dialog crash nullpointerexception android-edittext

在我的 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;
}

还有日志猫:

image 1

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

最佳答案

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

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;

关于android - 添加输入类型时 EditText 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9349839/

相关文章:

javascript - phonegap javascript 警报不起作用?

android - 如何绕过点击外的android对话框

c - 每次执行计数排序都会使我的程序崩溃,无法发现 malloc/free 错误

android - 对话框中忽略的 Html 格式标记

javascript - 具有不同内容的 JQuery 可重用对话框

android - 启动屏幕后我的应用程序崩溃

windows-phone-7 - Windows 8 x64 在 wp7 模拟器加载时崩溃

android - 使用按钮在 Android 上从布局 1 更改为布局 2

android - 客户端应用程序架构

java - 访问 NetworkImageView 的图像路径