Toast类型中的方法makeText不适用于参数

时间:2012-12-09 13:38:24

标签: android android-fragments android-viewpager toast

当我们滚动到下一个片段页面时,我正在使用ViewPageIndicator并尝试使用Toast来显示。

我得到他跟随错误

The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (PlaceDetailsFragment, String, int)

PlaceDetailsFragment.java

public class PlaceDetailsFragment extends SherlockFragment {
    ImageFragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mIndicator;

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

        mAdapter = new ImageFragmentAdapter(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(PlaceDetailsFragment.this, "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;
    }

}

3 个答案:

答案 0 :(得分:14)

片段不会扩展上下文。您必须将活动作为上下文传递。

Toast.makeText(PlaceDetailsFragment.this.getActivity(), "Changed to page " + position, Toast.LENGTH_SHORT).show();

答案 1 :(得分:1)

PearsonArtFoto已经给你答案了,但我想补充一些信息: 你必须使用getActivity(),因为你没有在Activity中调用它(或者扩展Activity的东西)。 Activity是一个上下文(它扩展了上下文),片段没有。

答案 2 :(得分:0)

使用片段

的容器参数
Toast.makeText(container.getContext(), picture, Toast.LENGTH_LONG).show();