The method makeText in the type Toast is not appli

2020-03-25 15:48发布

I am using a ViewPageIndicator and trying to use a Toast to display when we scroll to the next fragment page.

i am getting he following error

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条回答
▲ chillily
2楼-- · 2020-03-25 16:29

use container parameter of fragment

Toast.makeText(container.getContext(), picture, Toast.LENGTH_LONG).show();
查看更多
姐就是有狂的资本
3楼-- · 2020-03-25 16:45

Fragments don't extend context. You have to get the activity to pass as the context.

Toast.makeText(PlaceDetailsFragment.this.getActivity(), "Changed to page " + position, Toast.LENGTH_SHORT).show();
查看更多
可以哭但决不认输i
4楼-- · 2020-03-25 16:51

PearsonArtFoto already gave you the answer, but I like to add some information: You have to use getActivity() since you are not calling this inside an Activity(or something that extends Activity). Activity is a context(it extends context), fragment does not.

查看更多
登录 后发表回答