Jakub Mocarski
Jakub Mocarski

Reputation: 11

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?

Upvotes: 1

Views: 1840

Answers (1)

edison gudiño
edison gudiño

Reputation: 101

Check your activity's xml file , mybutton should be another view component.

Your xml view button should be like this:

<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.

Upvotes: 1

Related Questions