Reputation: 832
First of all, I found hundred of question on this Topic and solution but none of them help me. because I m doing little different workaround.
FragmentPagerAdapter class constructor
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
public class HomePage_Slider extends FragmentPagerAdapter {
public HomePage_Slider(FragmentManager fragmentManager) {
super(fragmentManager);
MyArr.addAll(globalVariable.slider_image_url);
mCount =MyArr.size();
}
public HomePage_Slider(android.app.FragmentManager fragmentManager) {
super(fragmentManager);
}
}
Fragment class
import android.app.Fragment;
public class host extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.content_navigation_drawer, container, false);
Top_slider = rootview.findViewById(R.id.home_slider_image);
viewPager = (ViewPager) Top_slider.findViewById(R.id.pager);
viewPager.setAdapter(new HomePage_Slider(getActivity().getFragmentManager()));
}
}
I am using Navigation drawer using Support library and follow this tutorial Navigation Drawer Tutorial . and trying to use FragmentPagerAdapter
to display slider. and My Host Fragment class uses import android.app.Fragment;
and FragmentPagerAdapter class uses import android.support.v4.app.Fragment;
Because of My host Fragment class do not import import android.support.v4.app.Fragment;
FragmentPagerAdapter constructor giving error FragmentPagerAdapter android.support.v4.app.fragment can not be applied to android.ap.Fragment
while searching solution I found this changing my import will do the trick but my rest app will stop working. used import android.app.Fragment;
to do other work like calling other Fragment class transaction something like this
//Host Fragment have this code to swap to next Fragment on click event on listview
((MainActivity)getActivity()).category_product_page();
// class contains
MainActivity.java
// go to wishlist page
public void category_product_page() {
// Create new fragment and transaction
cart_product newFragment = new cart_product ();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.frame_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
and I also did some other work on host Fragment that's why I can not change import android.app.Fragment "
to import android.support.v4.app.Fragment;
any suggestion will be helpful.
Please suggest how can I overcome this error and prevent these error in future as my second app have slider
Upvotes: 0
Views: 1425
Reputation: 75788
FragmentPagerAdapter android.support.v4.app.fragment can not be applied to android.ap.Fragment
When you are using android.support.v4.app.FragmentManager then you should use getSupportFragmentManager() and if you are using android.app.FragmentManager then call getFragmentManager()
You are using import android.support.v4.app.Fragment;
Don't
viewPager.setAdapter(new HomePage_Slider(getActivity().getFragmentManager()));
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Do
viewPager.setAdapter(new HomePage_Slider(getActivity().getSupportFragmentManager()));
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
You should call getSupportFragmentManager ()
instead of getFragmentManager()
.
Return the FragmentManager for interacting with fragments associated with this activity.
Upvotes: 3
Reputation: 76
You can not use 2 different libraries for same thing. You should use v4.app or app.Fragment for your all fragments and fragment managers to get working result.
Upvotes: 1
Reputation: 18112
Change
import android.app.Fragment;
to
import android.support.v4.app.Fragment;
After that use
getSupportFragmentManager()
where you are using
getFragmentManager()
Note: Anywher you get error simply use support import statements.
Upvotes: 1
Reputation: 3851
getSupportFragmentManager()
instead of
getFragmentManager()
should do the trick.
Upvotes: 1