2011-05-11 41 views
2

我对我的第二项活动(主),像这样使用一个意图:在列表onItemClick

Login -> Main -> Vforum 

我设法使用意图,像这样在登录活动去的主要活动:

Intent logMeIn = new Intent(this,Main.class); 
startActivity(logMeIn); 

工作正常。我现在的问题是从Main到Vforum。

projectList.setOnItemClickListener(new OnItemClickListener() 
{ 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    { 
     Intent launchVforum = new Intent(this, Vforum.class); 
     startActivity(launchVforum); 
    } 
}); 

projectListListView。 Eclipse是说:

The constructor Intent(new AdapterView.OnItemClickListener(){}, Class<Vforum>) is undefined 

,我不知道要放什么东西在那里this是解决它。我只想参加我的第三项活动(Vforum)。

回答

9

是的。曾经有类似的问题。我的解决办法是做到以下几点(使用你的例子):

- 在您的主要活动放在私有上下文中,像这样:

private Context mCtx; 

- 在您的主要活动onCreate()方法把此行的地方:

mCtx = this; 

- 当创建意图使用mCtx,而不是这样的:

Intent launchVforum = new Intent(mCtx, Vforum.class); 
+3

为了解释,问题是第一个参数需要是一个Context对象。你也可以尝试Main.this,如果你不想保存上下文 – dmon 2011-05-12 00:42:51

+0

谢谢你们,你们的解决方案都可以工作 – Ronnie 2011-05-12 17:32:36

3
projectList.setOnItemClickListener(new OnItemClickListener() 
{ 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    { 
     Intent launchVforum = new Intent(YourActivity.this, Vforum.class); 
     startActivity(launchVforum); 
    } 
}); 
相关问题