您的位置:首页 > 移动开发 > Android开发

关于Android中“OnClickLinstener cannot be resolved to a type”的错误

2016-01-06 20:02 405 查看
在如下的Android代码中可能会出现一些错误

<span style="font-size:18px;">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() {
@Override
public void onClick(View v){
Toast.makeText(FirstActivity.this, "You cliced Button 1", Toast.LENGTH_SHORT).show();

}
});
}</span>
会出现两个错误:



第一个错误提示是:OnClickListener cannot be resolved to a type

第二个错误提示是:The method onClick(View) of type new OnClickListener(){} must override or implement a supertype method

解决方案:1,将代码“button1.setOnClickListener(new OnClickListener() {……}”中的OnClickListener()改为View.OnClickListener()即可,;

                    2,或者在开头添加import android.view.View.*;

(以上代码来源于郭霖的《第一行代码》第37页)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android