I looked through quite a lot of code and can't figure it out. http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html
It should be something simple. I show most of the code. The error is given in the next section.
public class MainActivity extends FragmentActivity implements ActionBar.TabListener { public static final int TAB_COUNT = 3; public static InputMethodManager inputManager; SectionsPagerAdapter mSectionsPagerAdapter; ViewPager mViewPager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSectionsPagerAdapter = new SectionsPagerAdapter( getSupportFragmentManager());
That is where my mistake is. case 1 of getItem , Type Mismatch: Unable to convert from MainActivity.HistoryFragment to Fragment. he tells me to either change the return type of the method to HistoryFragment , or change the return type of newInstance() to a fragment. Where I can't either. Other examples that I saw are almost identical to my code. I tried with and without argument.
public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { switch (i) { case 0: Fragment tipCalc = new TipCalcFragment(); return tipCalc; case 1:
And my HistoryFragment , which extends ListFragment . As a result, it will not be drawn from String[] values, but from database resources. I just wanted to set up the structure first and see it / play with the layout.
public static class HistoryFragment extends ListFragment { // public HistoryFragment() { // } String[] values = new String[] { ... }; static HistoryFragment newInstance(int num) { HistoryFragment history = new HistoryFragment(); Bundle args = new Bundle(); args.putInt("num", num); history.setArguments(args); return history; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.history, container, false); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, values)); } }
user1235035
source share