需要帮助理解 logcat 消息 ClassCastException

Need help understanding logcat message ClassCastException

    final EditText edt =findViewById(R.id.type_text);
    final TextView txt= findViewById(R.id.empty_text);
    Button   btn =      findViewById(R.id.button_add);
    final String value = edt.getText().toString();

    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            txt.append(value+"\n");
            edt.setText("");
        }
    });

这是第 40 行 --> 最终的 EditText edt =findViewById(R.id.type_text);

Logcat 留言
Caused by: java.lang.ClassCastException: android.support.design.widget.TextInputLayout cannot be cast to android.widget.EditText at com.example.todo.activitity2.onCreate(activitity2.java:40)

在您的布局 XML 文件中有 ID 为 type_textTextInputLayout,您正试图将其转换为 EditText.

在您的 XML 文件中将此 TextInputLayout 更改为 EditText

在您的 Class.java 文件中更正此: final EditText edt = findViewById(R.id.type_text);

final TextInputLayout your_name = findViewById(R.id.type_text);

你需要改变这个:

final EditText edt = findViewById(R.id.type_text);

进入正确的 class : (R.id.type_text 是 TextInputLayout 并且您将其创建为 EditText 这就是导致异常的原因)

final TextInputLayout edt = findViewById(R.id.type_text);