I use the following to select which fragment to display within my activity....
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment;
switch (position) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
default:
fragment = null;
break;
}
return fragment;
}
... a blank and working fragment looks like....
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_dod, container, false);
// Do Stuff Here
return root;
}
@Override public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save some state!
}
}
How ever my SectionsPagerAdapter errors with type mismatch: cannot convert from Fragment2 to Fragment when using a ListFragment like the following.....
public class Fragment2 extends ListFragment {
DemoAdapter adapter=null;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
if (adapter==null) {
ArrayList<Integer> items=new ArrayList<Integer>();
for (int i=0;i<25;i++) { items.add(i); }
adapter=new DemoAdapter(getActivity(), items);
}
else {
adapter.startProgressAnimation();
}
setListAdapter(adapter);
}
}
I have been looking for a way to get round this. Could some one advise a way to do so and if possible an explanation so I understand why and can try and avoid it in the future ?
Thanks
The answer to my problem can be found here - Why can't I instantiate this Fragment on Android?
Figured out the issue.... my MainActivity.java file was using:
import android.support.v4.app.Fragment; and my MainFragment.java file was using:
import android.app.Fragment; So there was a mismatch. I am using ICS 4.0 and higher as my lowest API version. Changing everything to import to the v4 API solved my issue