用面包里面片段片段、面包、里面

2023-09-12 22:18:15 作者:短发也美

我试图表现出吐司消息,当用户在一个片段内点击一个按钮。问题是我无法访问活动,以显示吐司就可以了。

I'm trying to show a Toast Message when user click on a Button inside a Fragment. The problem is I cannot access the activity to show the Toast on it.

下面的片段来源:

    public class FrgTimes extends Fragment
    {
        ScrollView sv;
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) 
        {
            if (container == null) { return null; }

            sv = (ScrollView)inflater.inflate(R.layout.frg_times, container, false);

            btnTime1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

            //******  HERE's the PROBLEM  ********
            Toast.makeText(<The Activity>, "Please long press the key", Toast.LENGTH_LONG );

            }});

            return sv;
        }

和这里就是我一直在尝试了。

and Here's what I've been tried.

Toast.makeText( getActivity()  , ...
Toast.makeText( getView().getContext()  , ...
Toast.makeText( getActivity().getApplicationContext()  , ...
Toast.makeText( sv.getContext()  , ...
Toast.makeText( sv.getRootView().getContext()  , ...

在调试我可以看到,所有这些codeS运行没有任何异常,但没有土司显示。

In Debug I can see that all of these codes run without any exception but no TOAST being displayed.

推荐答案

你是不是叫显示()吐司要创建一个使用 makeText()

You are not calling show() on the Toast you are creating with makeText().