I'm trying to create an application in which you can flip through the pages and read something. Found an article on the off site here is the link . It says to create a nested class that will prepare us fragments for display on the screen (if I understood everything correctly), did everything according to the instructions, but the studio swears at this line return new FragmentSlide();

Here is the class itself:

  import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; public class MainActivity extends FragmentActivity { private static final int NUM_PAGES = 5; private ViewPager mViewPager; private PagerAdapter mPagerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mViewPager = (ViewPager) findViewById(R.id.pager); mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager()); mViewPager.setAdapter(mPagerAdapter); } public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm){ super(fm); } @Override public Fragment getItem(int position) { return FragmentSlide.create(position); } @Override public int getCount() { return NUM_PAGES; } } } 

Error itself

 Error:(40, 20) error: incompatible types: FragmentSlide cannot be converted to Fragment 

I tried to replace the libraries with app.Fragment and app.FragmentManager did not help. It is strange that in the example that I downloaded from there, they use them, although the site uses the android.support.v4.app.Fragment in the example with the code for this Activity; and the same FragmentManager, I tried both options, did not help. How to bring these types, so that the studio does not swear?

Fragment class:

 import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class FragmentSlide extends Fragment { public static FragmentSlide create(int pageNumber){ FragmentSlide fragmentSlide = new FragmentSlide(); Bundle args = new Bundle(); args.putInt("page", pageNumber); fragmentSlide.setArguments(args); return fragmentSlide; } public FragmentSlide(){ } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ViewGroup rootView = (ViewGroup) inflater. inflate(R.layout.fragment_slide_screen,container,false); return rootView; } } 
  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin

1 answer 1

You have a fragment not from the library of support. Replace the import in the FragmentSlide class with

  import android.support.v4.app.Fragment; 

instead

import android.app.Fragment;