2011-05-11 42 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); 
    } 
}); 
相關問題