Android:android.app.FragmentManager无法转换为android.support.v4.app.FragmentManager

时间:2016-08-13 18:50:58

标签: android android-fragments fragmentpageradapter

我试图为我的片段创建一个适配器,但我明白了:

错误:(19,72)错误:不兼容的类型:android.app.FragmentManager无法转换为android.support.v4.app.FragmentManager

这是我主要活动的一部分:

import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;

public class MainActivity extends Activity {

CustomPagerAdapter mCustomPagerAdapter;
ViewPager mViewPager;

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

    // == Setting up the ViewPager ==

    mCustomPagerAdapter = new CustomPagerAdapter(getFragmentManager(),this);    <--- Error is on this line

这是我的customAdapter类的一部分:

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class CustomPagerAdapter extends FragmentPagerAdapter {

protected Context mContext;

public CustomPagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    mContext = context;
}

我感谢任何帮助!

4 个答案:

答案 0 :(得分:1)

您需要在代码中使用SELECT ct.Quote_Date, ht.Quote_Date AS htDate, ct.Quote_Price, ht.Quote_Price AS [htPrice], ((ct.Quote_Price - ht.Quote_Price) / ht.Quote_Price) * 100 AS ChangePerc FROM #TempStock ct LEFT JOIN #TempStock ht ON CONVERT(DATE, CAST(ct.Quote_Date AS VARCHAR), 101) = DATEADD(MM, 1, CONVERT(DATE, CAST(ht.Quote_Date AS VARCHAR), 101)) ORDER BY ct.Quote_Date DESC ,而不是getSupportFragmentManager()。请参阅here原因

答案 1 :(得分:1)

您的 FragmentPagerAdapter 属于android.support.v4.app.FragmentPagerAdapter 要么将其更改为android.app.FragmentPageAdapter

或更改此

 mCustomPagerAdapter = new CustomPagerAdapter(getFragmentManager(),this);

mCustomPagerAdapter = new CustomPagerAdapter(getSupportFragmentManager(),this);

将解决您的问题干杯:)

答案 2 :(得分:0)

您的MainActivity应该延长FragmentActivity,您应该使用getSupportFragmentManager()方法而不是getFragmentManager()

答案 3 :(得分:0)

请使用getSupportFragmentManager();解决你的问题