Android片段和fragmentActivity的问题

时间:2014-05-01 13:41:03

标签: java android android-fragments

我创建了一个带有可滑动视图的标签布局,每个标签都是一个片段。因此,当我放置一个viewflipper时,我将标签更改为片段活动,因此适配器类中的错误表示无法将FollowerFragments()转换为片段。有什么帮助吗?

适配器类:

public Fragment getItem(int index) {


    switch (index) {
    case 0:
        // Followers fragment activity
        return new FollowersFragment();
    case 1:
        // Best fragment activity
        return new BestSellingFragment();
    case 2:
        // Hot fragment activity
        return new HotDealsFragment();
    }

    return null;
}

Followerfragment class:

public class FollowersFragment extends FragmentActivity {


public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_followers, container, false);

    return rootView;
}
ViewFlipper flipper;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Getting View Flipper from main.xml and assigning to flipper reference variable
    flipper=(ViewFlipper)findViewById(R.id.viewFlipper1);
    flipper.setAutoStart(true);
    flipper.setFlipInterval(500);
}
 }

2 个答案:

答案 0 :(得分:2)

public class FollowersFragment extends FragmentActivity {

它应该扩展Fragment,而不是FragmentActivity

FragmentActivityActivity,支持LoadersFragment。例如,通过FragmentActivity,您可以访问FramgentManagerLoaderManager。片段代表UI,它由FragmentActivity

托管

答案 1 :(得分:0)

FollowersFragment应该Fragment而不是FragmentActivity

为什么呢? FragmentActivity是包含片段的Activity,但它本身不是片段,因此无法转换。