简体   繁体   中英

Wrong First Argument Type

This is a sample of my imports

import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
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.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;

And this is where i use the Fragment

private void SetUpViewPager(ViewPager viewpager) {
    /*Creating a tab adapter*/
    TabAdapter adapter = new TabAdapter(getSupportFragmentManager());
    adapter.AddFragment(new Fragment1(),"Activity");
    adapter.AddFragment(new Fragment2(),"Friends");
    adapter.AddFragment(new Fragment3(),"Inbox");

    /*Set the adapter to the view pager property*/
    viewpager.setAdapter(adapter);
}

I get an error of Wrong First Argument found Fragment1 Required Fragment2

Here is the Adapter Class And i am not sure where the problem is
in the code
below private class TabAdapter extends FragmentPagerAdapter {

    private ArrayList<Fragment> Fragments;
    ArrayList<String> FragmentNames;

    /*Constructor*/
    TabAdapter(FragmentManager fm) {
        super(fm);
        Fragments = new ArrayList<>();
        FragmentNames = new ArrayList<>();
    }

    /*Helper method which adds a fragment which in turn adds a tab*/
    void AddFragment(Fragment1 fragment, String name) {
        Fragments.add(fragment);
        FragmentNames.add(name);
    }

    /*Get the current item of the tab its on*/
    @Override
    public Fragment getItem(int position) {
        return Fragments.get(position);
    }

    /*Returns the number of fragments that are in this adapter*/
    @Override
    public int getCount() {
        return Fragments.size();//Check on This
    }

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

AddFragment() is expecting Fragment1 as the first parameter. So the error is obvious.

/*Helper method which adds a fragment which in turn adds a tab*/
void AddFragment(Fragment1 fragment, String name) {
    Fragments.add(fragment);
    FragmentNames.add(name);
}

If you want to make your method accept multiple classes, then you have two options.

  1. Make AddFragment() accept Fragment as the first parameter

    addFragment(Fragment fragment, String name)

  2. Declare an interface and make your classes comply to that interface, and use the interface as the Parameter.

FragmentInterface.java

public interface FragmentInterface{
    // You can declare common functionalities here
}

Fragment1.java

public class Fragment1 extends Fragment implements FragmentInterface {
    // Class implementation
}

TabAdapter.java

/*Helper method which adds a fragment which in turn adds a tab*/
void AddFragment(FragmentInterface fragment, String name) {
    Fragments.add(fragment);
    FragmentNames.add(name);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM