【问题标题】:Unexpected implicit cast to Button: layout tag was TextView意外隐式转换为 Button:布局标记为 TextView
【发布时间】:2021-01-13 01:43:56
【问题描述】:

我无法得到它我已经尝试过 appcompat 之类的东西,但没有奏效。不知道怎么解决。

public class MainActivity extends AppCompatActivity {


    private Button button;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.showName);  ---> Here it is giving the problem.
        textView = findViewById(R.id.textView);
        textView.setText("Hello from Java!!");
    }
}

【问题讨论】:

  • 上传你的xml
  • 请发布您的 xml,然后我可以找出您的错误。
  • 既然你想通了,而且没有足够的信息来回答这个问题,请考虑删除你的问题。或者,edit您的问题以包含信息,然后发布您的答案。谢谢。
  • 是的,谢谢我明白了。它是 textView。我现在已将其转换为 Button。

标签: java android


【解决方案1】:

似乎 showName 是 TextView,而不是 Button,但我不确定。 请检查xml并将TextView更改为Button。

请上传您的 xml。

【讨论】:

  • 是的,谢谢,我知道了。首先是 textView,但我已将其转换为 Button。
【解决方案2】:

首先,Button 是 TextView,这就是为什么它将 Unexpected 隐式强制转换为 Button。 然后将其更改为 Button 后,它工作正常。

那么-->

<TextView
        android:id="@+id/showName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Name"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

现在-->

<Button
        android:id="@+id/showName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Name"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

在此之后它运行良好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多