Java – Android – Wrong first parameter type and ‘android.supportv4.app.fragment’

Android – Wrong first parameter type and ‘android.supportv4.app.fragment’… here is a solution to the problem.

Android – Wrong first parameter type and ‘android.supportv4.app.fragment’

I’m creating the Material Design tab View. I got an error after using the addFragment method on my main activity. This is the error I was experiencing.

Wrong 1st argument type. Found: 'com.example.sa.tabproject.LatestPromoFragment, ' android.supportv4.app.Fragment'

My MainActivity .java

import android.app.Activity;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.Toolbar;
import android.app.Fragment;

public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;

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

toolbar=(Toolbar)findViewById(R.id.toolBar);
    setSupportActionBar(toolbar);

tabLayout=(TabLayout)findViewById(R.id.tab_layout);

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

viewPagerAdapter= new ViewPagerAdapter(getSupportFragmentManager());

viewPagerAdapter.addFragment(new LatestPromoFragment(),"Latest Promos");  new LatestPromoFragment() should be in getItem() otherwise it will cause crashes
    viewPagerAdapter.addFragment(new MapViewFragment(),"Map View");  new MapViewFragment() should be in getItem() otherwise it will cause crashes
    viewPager.setAdapter(viewPagerAdapter);
    tabLayout.setupWithViewPager(viewPager);

}}

ViewPagerAdapter.java

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;

public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragment = new ArrayList<>();  this line can cause crashes
ArrayList<String> tabTitles= new ArrayList<>();

public void addFragment(Fragment fragment,String titles){ 
    this.fragment.add(fragment);  this line can cause crashes
    this.tabTitles.add(titles);

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

}

@Override
public Fragment getItem(int position) {
    return fragment.get(position);  this should create the Fragment instances
}

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

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

I imported several fragment classes into MainActivity.java. But it doesn’t fix the bug.

Solution

This is because in the adapter class you are using support

fragments, i.e. import android.support.v4.app.Fragment; in the Activity class, you are using app fragment import android.app.Fragment;

Use the same type of fragment everywhere you can work.

Related Problems and Solutions