我的代码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");
          }
        });
    }

解决方案 »

  1.   

    ibutton.setOnClickListener((OnClickListener) this);中的this表示对象本身,它所指的对像本身就是ibutton,即this=ibutton,而ibutton是TEST类型setOnClickListener(View.OnClickListener)的参数要求为View.OnClickListener类型,所以要想把ibutton传进去当参数,只能进行强转,这样才能编译通过。但不能运行,因为ibutton本质上是TEST类型,不能当作View.OnClickListener用的。
      

  2.   

    谢谢楼上,但ibutton应该是ImageButton类型.
    而我看到其他人的代码好象是可以用setOnClickListener(this)设置,并且应用能跑起来啊.
      

  3.   

    而且只要把这句ibutton.setOnClickListener((OnClickListener) this); 注释掉,就可以跑起应用了。
      

  4.   

    Class ImageButton
    java.lang.Object
      org.apache.wicket.Component
          org.apache.wicket.MarkupContainer
              org.apache.wicket.up.html.WebMarkupContainer
                  org.apache.wicket.up.html.form.LabeledWebMarkupContainer
                      org.apache.wicket.up.html.form.FormComponent
                          org.apache.wicket.up.html.form.Button
                              org.apache.wicket.up.html.form.ImageButton
    All Implemented Interfaces: 
    java.io.Serializable, IClusterable, IConverterLocator, IRequestListener, IResourceListener, IFormSubmittingComponent, IFormVisitorParticipant, ILabelProvider
      

  5.   

    找到方法解决了
    用public class TEST extends Activity 
        implements Button.OnClickListener{
    }即可
      

  6.   

    public static interface 
    View.OnClickListener
    android.view.View.OnClickListener 
      

  7.   

    呵,我刚想问你ImageButton 有没有实现OnClickListener 接口呢
      

  8.   

    我是才起步学java,以前一直做驱动的.真是对这有点摸不着头脑.希望大家多多指教.
    还有谢谢楼上的回复.
      

  9.   


    哈,谢谢啊,对java真是一窍不通啊,还云里雾里来.
      

  10.   

    我也刚搞明白,原来不加Button.OnClickListener,直接写OnClickListener它实现的不是Button的那个OnClickListener,所以就会这么纠结。
      

  11.   

    大侠,您好,我也碰到了和您同样的问题,并且看到了您的解决方法,但是我不知道原理是神马?大侠可以说说么。我的理解是如果import导入了那个包就不用implements OnClickListener再实现这个接口了。不过明显我的理解是错的。请大侠解惑。小弟拜谢了。
      

  12.   

    import android.view.View.OnClickListener;
      

  13.   

    我也想知道,而且加了implements Button.OnClickListener以后会发现import进来的包已经属于无效了。希望有大侠能指导一下!
      

  14.   

      加入这个implements OnClickListener{  后  还是会出现这样的错误 The type TestActivity must implement the inherited abstract method View.OnClickListener.onClick(View)。。
        求解!
      

  15.   

    我加了下面这个后就好了
    public void onClick(View v) {
    // TODO Auto-generated method stub

    }
      

  16.   

    Hellow android那本书就是这样说的!