当前位置:编程学习 > wap >>

activity跳转问题,求大神!

点击图片按钮,进行跳转
btnAbout.setOnClickListener(new OnClickListener(){

  public void onClick(View v) {  

              Intent intent=new Intent();
              intent.setClass(this,main2.class); 
             
    startActivity(intent);  
          }  
      });  
这段代码中对setclass报错,详情如下
The method setClass(Context, Class<?>) in the type Intent is not applicable for the arguments (new View.OnClickListener(){}, Class<main2>)
求大神指点迷津 --------------------编程问答--------------------
 intent.setClass(this,main2.class); 

这里面的this指的是new OnClickListener的对象实例
你应该指定的是当前Activity.this --------------------编程问答-------------------- this错了,这里的intent.setClass(this,main2.class)的this指的是view,要写这个activity名.this才对,还有后面那句,要写activity名.this.startActivity(intent),,, --------------------编程问答--------------------
引用 2 楼 heaimnmn 的回复:
this错了,这里的intent.setClass(this,main2.class)的this指的是view,要写这个activity名.this才对,还有后面那句,要写activity名.this.startActivity(intent),,,


正确     --------------------编程问答-------------------- LSD正解。 --------------------编程问答-------------------- 1楼和2楼说的都对。
this指针的问题,你要正确理解this指针。 --------------------编程问答--------------------
引用 5 楼 tomur 的回复:
1楼和2楼说的都对。
this指针的问题,你要正确理解this指针。

得清楚this指的是什么。。。 --------------------编程问答-------------------- new OnClickListener()  内部类,里面用intent  不能这样写,Intent intent=new Intent();
              intent.setClass(this,main2.class); 


--------------》》intent.setClass(TestClass.this,main2.class);  --------------------编程问答-------------------- The method setClass(Context, Class<?>) in the type Intent is not applicable for the arguments (new View.OnClickListener(){}, Class<main2>)


把里面的this改成你这个Activity.this就行了,如果只是一个this,它是指new View.OnClickListener(){},上面的错误提示里面已经说了 --------------------编程问答-------------------- 他们说的都对啊。。。。 --------------------编程问答--------------------               intent.setClass(Acticity.this,main2.class); 
直接this的话是引用onClick的实例 --------------------编程问答-------------------- --------------------编程问答-------------------- this错误,  应该用 (activity).this
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,