本文介绍了安卓getActivity()是未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的课MainActivity存在错误。

错误: 的方法getActivity()是未定义的类型MainActivity

 公共类MainActivity扩展SherlockFragmentActivity {
    PlaceSlidesFragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mIndicator;

    公共静态最终字符串变量=detailsFragment;

    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        查看查看= inflater.inflate(R.layout.main,
                集装箱,假);

        mAdapter =新PlaceSlidesFragmentAdapter(getActivity()
                .getSupportFragmentManager());

        mPager =(ViewPager)view.findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mIndicator =(CirclePageIndicator)view.findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
        ((CirclePageIndicator)mIndicator).setSnap(真正的);

        mIndicator
                .setOnPageChangeListener(新ViewPager.OnPageChangeListener(){
                    @覆盖
                    公共无效onPageSelected(INT位置){
                        Toast.makeText(MainActivity.this.getActivity(),
                                更改为网页+位置,
                                Toast.LENGTH_SHORT).show();
                    }

                    @覆盖
                    公共无效onPageScrolled(INT位置,
                            浮positionOffset,INT positionOffsetPixels){
                    }

                    @覆盖
                    公共无效onPageScrollStateChanged(INT状态){
                    }
                });
        返回查看;
    }
}
 

解决方案

这是因为你使用 getActivity()内部类中。请尝试使用:

SherlockFragmentActivity.this.getActivity()

来代替,但真的没有必要在 getActivity()部分。在你的情况, SherlockFragmentActivity。这就足够了。

I have errors in my class MainActivity.

The error: The method getActivity() is undefined for the type MainActivity

public class MainActivity extends SherlockFragmentActivity{
    PlaceSlidesFragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mIndicator;

    public static final String TAG = "detailsFragment";

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main,
                container, false);

        mAdapter = new PlaceSlidesFragmentAdapter(getActivity()
                .getSupportFragmentManager());

        mPager = (ViewPager) view.findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mIndicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
        ((CirclePageIndicator) mIndicator).setSnap(true);

        mIndicator
                .setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        Toast.makeText(MainActivity.this.getActivity(),
                                "Changed to page " + position,
                                Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onPageScrolled(int position,
                            float positionOffset, int positionOffsetPixels) {
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {
                    }
                });
        return view;
    }
}
解决方案

This is because you're using getActivity() inside an inner class. Try using:

SherlockFragmentActivity.this.getActivity()

instead, though there's really no need for the getActivity() part. In your case,SherlockFragmentActivity .this should suffice.

这篇关于安卓getActivity()是未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 08:38