I have an activity which manages four fragments via a ViewPagerAdapter. From my activity, I want to call a method:
public void openButtons(){
//mPosition is a position of pager
if (mPosition==0){
Fragment1 fragment = (Fragment1) getSupportFragmentManager().findFragmentById(R.id.fragment1);
fragment.openButtons();
}
if (mPosition==1){
Fragment2 fragment = (Fragment2) getSupportFragmentManager().findFragmentById(R.id.fragment2);
fragment.openButtons();
}
if (mPosition==2){
....
}
if (mPosition==3){
...
}
}
If the method in my fragment is defined as non-static:
public void openButtons(){//some stuff}
I get a nullpointer for fragment.openButtons () line and this whatever the position and the fragment.
If the method is declared as static, it's ok.
public static void openButtons(){//some stuff}
The content of the method is not in question because the problem is the same with an empty method.
So my question is why we have to define the static method in the fragment?
'Cause in these conditions:
public void openButtons(){
//mPosition is a position of pager
if (mPosition==0){
Fragment1.openButtons()
}
if (mPosition==1){
Fragment2.openButtons()
}
if (mPosition==2){
....
}
if (mPosition==3){
...
}
}
is equally functional!
Thanks.