I have a fragment 'X' whose parent activity is 'A'.
I have another activity 'B' which contains a method 'getData()'; This activity also has its own parameterized constructor in which object of Context is being passed to this constructor.
Activity B:-
public class B
{
Context _context;
String val="XYZ";
public B(Context context)
{
this._context = context;
//Rest of the code
}
public String getData()
{
return val;
}
}
My program requires me to call getData() method of activity 'B' in fragment 'X'
Fragment X :-
public class X extends Fragment implements
{
String name;
Context context=getActivity().getApplicationContext();
B obj1=new B(context);
//i have passed Context object from fragment X to Activity B
ButtonClickEvent(..)
{
name=obj1.getData();
//Rest of the code
}
}
The issue is arising while launching Fragment X; the error is related to Context object, I dont understand what exatcly is the issue; Suggestions are welcome! Thanks.