添加inputtype时EditText崩溃

时间:2012-02-19 14:02:59

标签: 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;
}

和logcat:

image 1

我知道它还没有存储EditText输入但是只要我添加setInputType行就会出现问题。

1 个答案:

答案 0 :(得分:0)

尝试将布局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;
相关问题