这段代码有问题:
{
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");
}
});
}
结果,我收到错误信息:
incompatible types: View cannot be converted to Button
我做错了什么?
最佳答案
检查您的 Activity 的 xml 文件,mybutton 应该是另一个 View 组件。
您的 xml View 按钮应该是这样的:
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mybutton" />
检查是否不是 ImageButton 或 ToggleButton。
关于java - 错误 : incompatible types: View cannot be converted to Button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53161658/