Hello! There is:

MyFragmentPagerAdapter

public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter { static final int PAGE_COUNT = 7; public MyFragmentPagerAdapter(FragmentManager fm) { super(fm); } @Override public int getCount() { return PAGE_COUNT; } @Override public Fragment getItem(int position) { return PageFragment.newInstance(position); } 

PageFragment:

 public class PageFragment extends Fragment { static final String ARGUMENT_PAGE_NUMBER = "arg_page_number"; public static final String PAGE_TEXT = "page text"; ArrayList<Predmet> itemList = new ArrayList<Predmet>(); MyAdapter boxAdapter; Button btnAdd, btnRead; DBHelper dbHelper; ListView myListView; Context ctx; int pageNumber; int backColor; public static PageFragment newInstance(int page) { PageFragment pageFragment = new PageFragment(); Bundle arguments = new Bundle(); arguments.putInt(ARGUMENT_PAGE_NUMBER, page); pageFragment.setArguments(arguments); return pageFragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); pageNumber = getArguments().getInt(ARGUMENT_PAGE_NUMBER); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_main, null); myListView = (ListView) view.findViewById(R.id.lvMain); // создаем адаптер fillData(); boxAdapter = new MyAdapter(ctx, itemList); // настраиваем список myListView.setItemsCanFocus(true); myListView.setAdapter(boxAdapter); return view; } //генерируем данные для адаптера void fillData() { for (int i = 0; i < 7; i++) { itemList.add(new Predmet(Integer.toString(i+1),"","")); }; } 

Underlines "return PageFragment.newInstance (position);" and writes an error: error: incompatible types required: Fragment found: PageFragment Please help me understand the cause of the error, I will be very grateful.

  • Check that in all cases the same fragment class is used. I mean from which library it is imported. \ - YuriySPb

1 answer 1

Check out the imports. If worth

 import android.app.Fragment; 

then replace it with

 import android.support.v4.app.Fragment; 
  • Thank! It helped!))) But now there is 1 more question. - roma
  • what question...? - Joe Silent
  • There was an error "Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService (java.lang.String) " and "lInflater = (LayoutInflater) ctx.getSystemService (Context.LAYOUT_INFLATER_SERVICE);" in the custom ListView adapter, as I understand that I am transmitting ctx with a value of null, but unfortunately I don’t understand how to fix it - roma
  • Thank you, I found a solution like that!))) - roma
  • Show the value of the variable ctx. - Joe Silent