无法在片段中工作

cannot work the toast in fragment

我想在我的应用程序的HomeFragment中开发一个搜索功能,但是它显示toast无法工作。

这是我的片段:

   /**
   * A simple {@link Fragment} subclass.
   */
public class HomeFragment extends Fragment {


public HomeFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    View rootView = inflater.inflate(R.layout.fragment_home, container, false);


    rootView.findViewById(R.id.btnSearch).setOnClickListener(new View.OnClickListener () {
        @Override
        public void onClick(View v) {
            new SimpleSearchDialogCompat<>(HomeFragment.this, "Search...", "What are you looking for...?",
                    null, initData(), new SearchResultListener<Searchable>() {
                @Override
                public void onSelected(BaseSearchDialogCompat baseSearchDialogCompat, Searchable searchable, int i) {
                    Toast.makeText(HomeFragment.this, "" + searchable.getTitle(), Toast.LENGTH_SHORT).show();
                    baseSearchDialogCompat.dismiss();
                }
            }).show();
        }

        // Inflate the layout for this fragment
        // return inflater.inflate(R.layout.fragment_home, container, false);
    });
    return rootView;
    }

private ArrayList<SearchModel> initData() {
    ArrayList<SearchModel>items =new ArrayList<>();
    items.add(new SearchModel("Captain America"));
    items.add(new SearchModel("Wonder Women"));
    items.add(new SearchModel("Batman"));
    items.add(new SearchModel("Iron Man"));
    items.add(new SearchModel("Hulk"));

    return items;
}

}

我试过每一个答案,例如:

Toast.makeText(getActivity(), "" + searchable.getTitle(), Toast.LENGTH_SHORT).show();

Toast.makeText(getActivity().getApplicationContext(), "" + searchable.getTitle(), Toast.LENGTH_SHORT).show();

Toast.makeText(HomeFragment.this.getActivity(), "" + searchable.getTitle(), Toast.LENGTH_SHORT).show();

但它仍然对我不起作用。

请试试这个:-

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    rootView.findViewById(R.id.btnSearch).setOnClickListener(new View.OnClickListener () {
        @Override
        public void onClick(View v) {
            new SimpleSearchDialogCompat<>(HomeFragment.this, "Search...", "What are you looking for...?",
                    null, initData(), new SearchResultListener<Searchable>() {
                @Override
                public void onSelected(BaseSearchDialogCompat baseSearchDialogCompat, Searchable searchable, int i) {
                    Toast.makeText(getActivity(), "" + searchable.getTitle(), Toast.LENGTH_SHORT).show();
                    baseSearchDialogCompat.dismiss();
                }
            }).show();
        }

        // Inflate the layout for this fragment
        // return inflater.inflate(R.layout.fragment_home, container, false);
    });
    return rootView;
    }

我已从您的代码中删除 super.onCreate(savedInstanceState);。您错误地将此代码放在 onCreateView() 方法中。