EeHang Tang
EeHang Tang

Reputation: 9

cannot work the toast in fragment

I want to develop a search function in the HomeFragment of my application, but it show me the toast cannot work.

Here is my fragment:

   /**
   * 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;
}

}

I had tried every answer like :

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

and

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

and

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

but it still cant worked for me.

Upvotes: 0

Views: 190

Answers (1)

Shalu T D
Shalu T D

Reputation: 4039

Please try this :-

@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;
    }

I have removed super.onCreate(savedInstanceState); from your code. You wrongly put this code in onCreateView() method.

Upvotes: 0

Related Questions