CircleView指标android错误(可能是新手错误)

时间:2016-10-02 15:22:32

标签: android android-studio

我是android dev的新手,我想实现一个Circle指标。 我正在遵循JakeWharton的指示:https://github.com/JakeWharton/ViewPagerIndicator#usage

但我收到了一个错误:

这是我的xml:

   <android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:animationCache="false" />

<com.viewpagerindicator.TitlePageIndicator
    android:id="@+id/titles"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" />

我的碎片:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        //Set the pager with an adapter
        ViewPager pager = (ViewPager)rootView.findViewById(R.id.container);
        pager.setAdapter(new TestAdapter(getSupportFragmentManager()));

        //Bind the title indicator to the adapter
        TitlePageIndicator titleIndicator = (TitlePageIndicator)rootView.findViewById(R.id.titles);
        titleIndicator.setViewPager(pager);
  .....
}

这一行:pager.setAdapter(new TestAdapter(getSupportFragmentManager()));

给我一​​个错误:无法从静态上下文引用非静态方法“getSupportFragmentManager()”。

我已经在爷爷那里进口了。 我该如何解决这个问题?谢谢你的建议

- 更新3:

我做了一个新的NON-STATIC课程,现在在新课程中:

//Set the pager with an adapter
        ViewPager pager = (ViewPager)rootView.findViewById(R.id.container);
        pager.setAdapter(new MainActivity.SectionsPagerAdapter(getActivity().getSupportFragmentManager()));

错误:MainActivity不是封闭类。

1 个答案:

答案 0 :(得分:0)

要在Fragment中使用SupportFragmentManager,您应该使用下一个语法getActivity().getSupportFragmentManager()