Home » Android » android – Using Context in FragmentPagerAdapter

android – Using Context in FragmentPagerAdapter

Posted by: admin June 15, 2020 Leave a comment


I couldn’t use context in FragmentPagerAdapter. RadioGroup(this) gives me undefined error. I used getContext() instead of this but couldn’t achieve it

private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {  

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

How to&Answers:

I am not sure why you are instantiating a RadioGroup in a FragmentPagerAdapter, but anyway you can get the context by modifying the constructor of the class:

private Context context; 

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

Then you can add the context when you create your FragmentPagerAdapter.