Search code examples
androidfragment-tab-host

setup() in FragmentTabHost cannot be applied to


I am trying to create a fragment which will consist of a tablayout so i am using FragmentTabHost , but setup() method in code is giving error as follows -

  public class PayFrag extends Fragment  {
    private FragmentTabHost mTabHost ;
    View mRoot;
    TabHost tabHost;
     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {

    mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(),getChildFragmentManager(),R.layout.payfrag);

lastline in the code gives error enter image description here

I've even updated the android.support-v4 in gradle - here is my module gradle

 apply plugin:     'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'

    defaultConfig {
        applicationId "com.example.sairav.app3"
        minSdkVersion 17
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),        'proguard-rules.pro'
       }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.android.support:support-v4:22.1.1' 
}

Solution

  • You have to import v4 support library instead of standard library. In imports at the top of your PayFrag Fragment add these two lines:

    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentTabHost;
    

    If you have android.app.Fragment imported, you can replace it.