Reputation: 769
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 Fragments.
I do however experience some errors that I keep returning to.
Depending on how I implement it, it always keep going back to an "Type mismatch" error: "cannot convert from android.support.v4.app.Fragment to android.app.Fragment". I have tried removing all import references to either, and this error appears when I only use android.support.v4.app.Fragment in TabListener, FragmentActivity and my two Fragments.
The error occurs in my TabListener:
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
public class TabListener implements ActionBar.TabListener {
private android.app.Fragment fragment;
private Activity activity;
private ViewPager pager;
private FragmentTransaction ft;
public TabListener(Activity activity, Fragment fragment, ViewPager pager) {
this.activity = activity;
this.fragment = fragment;
this.pager = pager;
}
@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft){
if (fragment == null) {
ft.add(fragment, null);
} else {
ft.attach(fragment);
}
}
@Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft){
// TODO Auto-generated method stub
}
@Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft){
// TODO Auto-generated method stub
}
}
By removing "android.app.FragmentTransaction ft", replacing it with just "FragmentTransaction ft", the problem goes awawy. Then new problems arise:
The method onTabReselected(ActionBar.Tab, FragmentTransaction) of type TabListener must override or implement a supertype method TabListener.java
The method onTabSelected(ActionBar.Tab, FragmentTransaction) of type TabListener must override or implement a supertype method TabListener.java
The method onTabUnselected(ActionBar.Tab, FragmentTransaction) of type TabListener must override or implement a supertype method TabListener.java
The type TabListener must implement the inherited abstract method ActionBar.TabListener.onTabReselected(ActionBar.Tab, FragmentTransaction) TabListener.java
The type TabListener must implement the inherited abstract method ActionBar.TabListener.onTabSelected(ActionBar.Tab, FragmentTransaction) TabListener.java
The type TabListener must implement the inherited abstract method ActionBar.TabListener.onTabUnselected(ActionBar.Tab, FragmentTransaction) TabListener.java
Whats going on here?
As you may understand, I'm new to Java and Android development. I feel like I'm pretty close, but I'm not able to solve this problem. I don't understand why it want to "convert from android.support.v4.app.Fragment to android.app.Fragment when I'm not even importing android.app.Fragment anywhere.
I guess it's related to using the compatibility package. (Do I have to use this package at all when creating an app for the newest version of the SDK?)
Upvotes: 45
Views: 78414
Reputation: 1
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()
Upvotes: 0
Reputation: 116
This solution works for me
replace
public class MyFragment extends Fragment{
}
with
public class MyFragment extends android.support.v4.app.Fragment{
}
and also replace import
import android.app.Fragment;
with
import android.support.v4.app.Fragment;
Upvotes: 4
Reputation: 19398
Try to use getSupportFragmentManager()
instead getFragmentManager()
Upvotes: 130
Reputation: 1458
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.
Upvotes: 4
Reputation: 1438
I had the same problem. Solved it by changing the interface from implements ActionBar.TabListener
to implements TabListener
and then implement the methods inside this interface. Its also mentioned the error you have mentioned.
Upvotes: 0
Reputation: 487
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.
Upvotes: 0
Reputation: 1006614
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.
Upvotes: 14
Reputation: 31846
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.
Upvotes: 0