Cannot convert from View to Button

后端 未结 3 970
一个人的身影
一个人的身影 2021-01-28 06:29

Very frustrating problem I have here. I have this code:

Button b = findViewById(android.R.id.button1);

And I\'m getting this error on it:

相关标签:
3条回答
  • 2021-01-28 06:53

    remove android.R from packages and import your R.

    import com.companyname.productname.R;
    

    and change also button refrence

    Button b = (Button)findViewById(R.id.button1);
                                    ^^^^^^^^^^^^
    
    0 讨论(0)
  • 2021-01-28 06:57

    You need to cast the view to Button:

    Button b = (Button) findViewById(android.R.id.button1);
    

    More details at http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

    In addition, as answered by others, the id is wrong.

    0 讨论(0)
  • 2021-01-28 07:08

    Your mistake is here-Button b = findViewById(android.R.id.button1);

    Replace the above line by findViewById(R.id.button1);

    0 讨论(0)
提交回复
热议问题