![](/img/trans.png)
[英]Error:(23, 39) error: incompatible types: View cannot be converted to Context
[英]Error: incompatible types: View cannot be converted to Button
我對這段代碼有問題:
{
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");
}
});
}
結果,我收到錯誤消息:
不兼容的類型:視圖無法轉換為按鈕
我究竟做錯了什么?
檢查您的活動的 xml 文件, mybutton 應該是另一個視圖組件。
你的 xml 視圖按鈕應該是這樣的:
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mybutton" />
檢查是否不是 ImageButton 或 ToggleButton。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.