Divyang Patel
Divyang Patel

Reputation: 347

The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, ListViewFragment)

I have error in listfragment "The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, ListFragment)" what should i do? cause i can't appear my fragment.

import android.app.Activity;
import android.content.Intent; 
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {



private FadingActionBarHelper mFadingActionBarHelper;

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

    mFadingActionBarHelper = new FadingActionBarHelper(getActionBar(),
            getResources().getDrawable(R.drawable.actionbar_bg));

    if (savedInstanceState == null) {
         getFragmentManager().beginTransaction()
                .add(R.id.container, new ListViewFragment())
                .commit();

    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    }
    return super.onOptionsItemSelected(item);
}

public FadingActionBarHelper getFadingActionBarHelper() {
    return mFadingActionBarHelper;
}

}

The problem is that the FragmentTransaction 's .add(int, Fragment) method accept arguments with the second one of type Fragment, however, I have ListFragment which is not acceptable by this method.

So, how to add ListFragment with FragmentTransaction ?

Here ListView Fragement class:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import com.viewpagerindicator.TabPageIndicator;

public class ListViewFragment extends FragmentActivity {
private static final String[] CONTENT = new String[] { "Recent", "Artists",
        "Albums", "Songs", "Playlists", "Genres", "Hello", "Hii" };

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

    FragmentPagerAdapter adapter = new GoogleMusicAdapter(
            getSupportFragmentManager());

    ViewPager pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
    indicator.setViewPager(pager);
}

class GoogleMusicAdapter extends FragmentPagerAdapter {
    public GoogleMusicAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return CONTENT[position % CONTENT.length].toUpperCase();
    }

    @Override
    public int getCount() {
        return CONTENT.length;

    }
}

Upvotes: 0

Views: 3700

Answers (1)

zozelfelfo
zozelfelfo

Reputation: 3776

Maybe I am wrong but without your ListViewFragment code I will assume that some of these are the problem you are finding.

  1. (Not very likely) Your ListViewFragment is not a subclass of Fragment (or ListFragment).

  2. Your ListViewFragment is a subclass of android.support.v4.app.Fragment (check your imports) instead of being a subclass of android.app.Fragment. If this is the case you have 2 possibilities.

2.1 Change your import to android.app.Fragment

2.2 Make your Activity a subclass of FragmentActivity and use getSupportFragmentManager() instead of getFragmentManager().

If neither of these are your problem, please post your ListViewFragment class.

Hope it helps :)

Upvotes: 7

Related Questions