简体   繁体   English

错误:不兼容的类型:视图无法转换为按钮

[英]Error: incompatible types: View cannot be converted to Button

I have a problem with this code:我对这段代码有问题:

{
    TextView txt;
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt=(TextView) findViewById(R.id.mytext);
        btn=(Button) findViewById(R.id.mybutton);

        btn.setOnClickListener (new View.OnClickListener(){
            @Override
            public void onClick(View V){
                txt.setText("TAK");
            }
        });
}

As result, I get error that says:结果,我收到错误消息:

incompatible types: View cannot be converted to Button不兼容的类型:视图无法转换为按钮

What am I doing wrong?我究竟做错了什么?

Check your activity's xml file , mybutton should be another view component.检查您的活动的 xml 文件, mybutton 应该是另一个视图组件。

Your xml view button should be like this:你的 xml 视图按钮应该是这样的:

<Button
   android:text="Button"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/mybutton" />

check if is not an ImageButton or ToggleButton.检查是否不是 ImageButton 或 ToggleButton。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM