为什么用setOnClickListener(this)报错:in the type View is not applicable for the argume
我的代码
public class TEST extends Activity {
ImageButton ibutton ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initResourceRefs();
}
private void initResourceRefs() {
ibutton = (ImageButton)findViewById(R.id.button);
ibutton.setOnClickListener((OnClickListener) this);
}
public void OnClick(View v) {
if (!v.isEnabled()) {
return;
}
switch (v.getId()) {
case R.id.button:
Log.v("TEST", "on Click");
break;
}
}
}
我的疑问是这样的:
1、为什么用ibutton.setOnClickListener((OnClickListener) this);是可以编译过的,而用ibutton.setOnClickListener(this);报错The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (TEST);为什么一定要强制转换类型啊?
2、为什么用ibutton.setOnClickListener((OnClickListener) this);语句就不能在虚拟机上运行应用,报异常错误。
但如下写,就可以运行应用程序
private void test func() {
ibutton = (ImageButton)findViewById(R.id.button);
ibutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.v("TEST", "on Click");
}
});
}