0

I want to show toast on button click, and that button is in the fragment. I have tried methods to get context for the toast, but it is not showing on button click.

This is my code

public class Bottom_Sheet_Fragment extends Fragment {

Button addComment;

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


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


    View view=inflater.inflate(R.layout.fragment_bottom__sheet, container, false);

    addComment=(Button) container.findViewById(R.id.addCommentBtn);
    addComment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Toast.makeText(getActivity(), "Added", Toast.LENGTH_SHORT).show();
        }
    });

    return view;
  }
}

This is fragment layout

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
android:fillViewport="true"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_light"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/commentList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/etComment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:hint="Write comment" />

        <Button
            android:id="@+id/addCommentBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:text="Add" />
    </RelativeLayout>
</LinearLayout>

this layout file is included in another activity, with in include statement.dont know where is the problem. and this fragment works like a bottomsheet.

MissAmna
  • 317
  • 5
  • 13
  • tried adding some `Log.d` inside `onClick` and watch the logcat? – pskink Mar 15 '18 at 15:30
  • 1
    You need to call `findViewById()` on `view`, not `container`. I'm surprised that's not crashing. – Mike M. Mar 15 '18 at 15:31
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Selvin Mar 15 '18 at 15:31
  • @MikeM. I bet it does - but he decide to not write about it ... eventually, this is not the fragment which he is using ..... **or .... containter contains another button with the same id but he is clicking the one from fragment** – Selvin Mar 15 '18 at 15:32
  • @Mike M. i also tried 'findViewById' with view.but still it is not working. – MissAmna Mar 15 '18 at 15:37
  • @Selvin my application is not crashing. – MissAmna Mar 15 '18 at 15:38
  • Then the problem is not here... – Selvin Mar 15 '18 at 15:42
  • so what do you see on the logcat? do you see the `Log.d` you added inside `onClick` method? – pskink Mar 15 '18 at 15:44
  • log message is not displaying.it means onclick is not working – MissAmna Mar 15 '18 at 15:48
  • so try to add `Log.d` before calling `addComment.setOnClickListener`, do you see it now? – pskink Mar 15 '18 at 15:51
  • no.still not showing – MissAmna Mar 15 '18 at 15:58
  • 1
    so your fragment is not added at all: `Fragment#onCreateView` method is not called, do you know how to [debug your app](https://developer.android.com/studio/debug/index.html)? – pskink Mar 15 '18 at 15:59
  • 1
    and yes, it explains why your code didnt crash - once you add your fragment you will see the beautiful crash due to `NullPointerException` – pskink Mar 15 '18 at 16:05

2 Answers2

0

Try this: addComment = view.findViewById(R.id.addCommentBtn); Because you must get button from layout was inflated.

Tung Tran
  • 2,885
  • 2
  • 17
  • 24
-1

You need to use view.findViewById.

Try This

addComment=(Button) view.findViewById(R.id.addCommentBtn);
addComment.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        Toast.makeText(getActivity(), "Added", Toast.LENGTH_SHORT).show();
    }
});
yatin deokar
  • 730
  • 11
  • 20