需要帮助了解logcat消息ClassCastException

时间:2019-03-31 09:02:44

标签: java android classcastexception

def main():
    global update, direction, snake_pos, snake_speed, apple_spawn, apple_pos
    pygame.time.delay(60)

    rungame = True
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            rungame = False

    # [...]

    collision = False
    if snake_pos[0] < 0 or snake_pos[0] > screen_width-20:
        collision = True
    if snake_pos[1] < 0 or snake_pos[1] > screen_height-20:
        collision = True

    for block in snake_body[1:]:
        if snake_pos[0] == block[0] and snake_pos[1] == block[1]:
            collision = True

    score(scoreval)
    pygame.display.update()
    fps_controller.tick(25)

    return rungame, collision

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

Logcat消息
     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(""); } });

2 个答案:

答案 0 :(得分:2)

在布局XML文件中,存在ID为type_text TextInputLayout ,您正尝试将其转换为 EditText

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

OR

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

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

答案 1 :(得分:1)

您需要更改此内容:

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

进入适当的类:(R.id.type_text为TextInputLayout,您将其创建为EditText,这就是导致该异常的原因)

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