0

I am working on Android Project where I need to give a tabs in a page, So I am using Fragments for the same, I have successfully created fragments, But in the main page where I've code to join all fragment, its giving error that

'Wrong 1st argument type. Found: 'com.android.mads.madses.LoginInfoFragment', required: 'android.support.v4.app.Fragment' less... addFrag (android.support.v4.app.Fragment, String) in ViewPagerAdapter cannot be applied to (com.android.mads.madses.LoginInfoFragment, String)'

I am not getting what exactly the error it. Can anyone help me out with this please?

Here is the complete java code

  package com.android.mads.madses;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import java.util.ArrayList;
import java.util.List;

import com.android.mads.madses.LoginInfoFragment;
import com.android.mads.madses.VechicleInfoFragment;
import com.android.mads.madses.PersonalInfoFragment;
import com.android.mads.madses.ContactInfoFragment;


public class RegisterActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        adapter.addFrag(new LoginInfoFragment(), "LoginInfo");
        adapter.addFrag(new PersonalInfoFragment(), "PersonalInfo");
        adapter.addFrag(new ContactInfoFragment(), "ContactInfo");
        adapter.addFrag(new VechicleInfoFragment(), "VechicleInfo");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

}

1 Answer 1

1

Check the import statements for LoginInfoFragment, ensure it is using 'import android.support.v4.app.Fragment;'

Explanation:

android.support.v4.app.Fragment

This is from android support library which allows older versions of android to use newer features of Android. This allows backwards compatibility.

android.app.Fragment

This is the real implementation of Fragment and only supports Android (3.0+) I believe.

Use support.v4 if you want your app to support older versions.

Sign up to request clarification or add additional context in comments.

1 Comment

Can You please explain the difference between two?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.