setOnClickListener()与报错OnClickListener()原因
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.first_layout);
Button button1 = (Button) findViewById(R.id.button_1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Toast.makeText(FirstActivity.this,"You are clicked Button 1",
Toast.LENGTH_SHORT).show();
}
});
}
程序报错,并提示
Multiple markers at this line
- OnClickListener cannot be resolved to a type
- The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})
后发现是导入android.view.View;时,并不会导入android.view.View.OnClickListener,需要手动导入这个包。

本文详细介绍了在Android开发过程中遇到的一个常见问题:在Activity的onCreate方法中设置按钮的点击监听器时出现的类型不匹配错误。通过分析问题原因并提供解决方案,帮助开发者避免此类错误,提升开发效率。
2752





