2013-04-20 26 views
6

我無法在FragmentPagerAdapter中使用上下文。 RadioGroup(this)給我一個未定義的錯誤。我用getContext()代替這一點,但不能達到它在FragmentPagerAdapter中使用上下文

private static class MyFragmentPagerAdapter extends FragmentPagerAdapter { 

final RadioGroup rg = new RadioGroup(this); // what can I use instead of "this" ? 

} 

回答

16

我不知道你爲什麼被實例化的FragmentPagerAdapter一個RadioGroup中,但無論如何,你可以通過修改類的構造函數獲取上下文:

private Context context; 

/** Constructor of the class */ 
public MyFragmentPagerAdapter(FragmentManager fm, Context c) { 
    super(fm); 
    context = c; 
} 

然後,您可以在創建FragmentPagerAdapter時添加上下文。