2013-04-20 55 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时添加上下文。