Search code examples
androidaudiosoundpool

App stop working after button click


so I´m making an app with fragments and I want a sound on button click. Everything works except when I click on the button the app stop working. I put an onclick method named playAnother on the button. I think the problem might be there in .java file: here is screen I dont know what to do I´m total beginner. Have a nice day!


Solution

  • The method playAnother(View view) must be in your Activity, not in fragment.

    OR

    If you want to handle the button click in your fragment you can give your button an id:

    <Button
      android:id="@+id/button"
      .
      .
    

    And then in your fragment's onCreateView()

    public View onCreateView(...) {
        // First we save the reference to the views of your fragment
        View view = inflater.inflate(R.layout.fragment_three, container, false);
    
        // Then we need to find the button-view
        Button button = (Button) view.findViewById(R.id.button);
        // And finally we can register OnClickListener for the button
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Handle your button click here
            }
        });
        return view;
    }