Cannot convert from android.support.v4.app.Fragment to android.app.Fragment

后端 未结 8 1544
南笙
南笙 2020-12-10 00:41

I\'m doing my first Android app, and wanted to get straight into the ICS API. I have so far created an app using an ActionBar, with swipeable tabs using Viewpager and Fragme

相关标签:
8条回答
  • 2020-12-10 00:49

    You can remove the support package, and that should solve your problem. It is only needed when you need functions from Android 3.0 and above in apps for earlier versions.
    In your case you get both the default Fragments from ICS, and the Fragments from the support package, and if you happen to get objects from the different packages they will not work together.

    Short version; You use either an api level above Honecomb or the support package, not both.

    0 讨论(0)
  • 2020-12-10 00:49

    look here:fragmentTransaction.add(R.id.main_container, new HomeFragment()); you add the fragment ,but follow,you use replace() method ,so you should use replace instead of add()

    0 讨论(0)
  • 2020-12-10 00:54

    I know that it has been too late to answer this question but it might help someone with the same problem.

    Go to your java folder and click on your fragment's activity.

    In the imports, replace import android.app.Fragment; with

    import android.support.v4.app.Fragment;

    Keep the code in the MainActivity intact and this should help resolve the issue.

    Note: If it doesn't work at once, don't worry. Build > Rebuild project.

    0 讨论(0)
  • 2020-12-10 00:55

    I've had the same problem yesterday.

    There is a really nice page by Samsung that covers ActionBarSherlock. Check if you use one of the imports/classes/methods on the left and replace them by the imports/classes/methods on the right.

    0 讨论(0)
  • 2020-12-10 01:13

    Try to use getSupportFragmentManager() instead getFragmentManager()

    0 讨论(0)
  • 2020-12-10 01:13

    Whats going on here?

    While the Android Support package gives you a backwards-compatible Fragment implementation, the ActionBar is not part of the Android Support package. Hence, ActionBar.TabListener is expecting native API Level 11 Fragment objects. Consider using ActionBarSherlock to have both an action bar and Android Support fragments.

    but then I'm left with another problem in my FragmentPagerAdapter

    The FragmentPagerAdapter in the Android Support package is a bit messy -- it wants API Level 11 Fragment objects, not Android Support Fragment objects. However, you can clone the source to FragmentPagerAdapter (source is in your SDK) and create your own implementation that uses the support.v4 flavor of Fragment and kin.

    0 讨论(0)
提交回复
热议问题