FragmentTransaction类型中的方法add(int,Fragment)不适用于参数(int,ListViewFragment)

时间:2014-12-17 06:07:16

标签: android android-fragments fragment android-listfragment

我在listfragment中有错误“FragmentTransaction类型中的方法replace(int,Fragment)不适用于参数(int,ListFragment)”我该怎么办?因为我不能出现我的片段。

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;
}

}

问题是FragmentTransaction的.add(int,Fragment)方法接受带有第二个Fragment类型的参数,但是,我有ListFragment这个方法不能接受。

那么,如何使用FragmentTransaction添加ListFragment?

这里是ListView Fragement类:

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;

    }
}

1 个答案:

答案 0 :(得分:7)

也许我错了但没有你的ListViewFragment代码,我会假设其中一些是你找到的问题。

  1. (不太可能)您的ListViewFragment不是Fragment(或ListFragment)的子类。

  2. 您的ListViewFragmentandroid.support.v4.app.Fragment的子类(检查您的导入),而不是android.app.Fragment的子类。如果是这种情况,您有两种可能性。

  3. 2.1将导入更改为android.app.Fragment

    2.2将您的活动设为FragmentActivity的子类,并使用getSupportFragmentManager()代替getFragmentManager()

    如果这些都不是您的问题,请发布您的ListViewFragment课程。

    希望有所帮助:)