Error placing Imagebuttons with sounds inside Ragment

Asked

Viewed 40 times

1

I’m trying to put Imagebuttons playing sounds inside a Fragment called Tab1, but I’m finding two errors:

Non-static method 'findViewById(int)' cannot be referenced from a Static context

on the line

final ImageButton vitasbut = tab1.findViewById(R.id.vitasbut);

and

Cannot resolve method 'create(com.example.mateuspc1.memepocket.Mainactivity.Tab1, int)'

on the lines mediaPlayer = MediaPlayer.create(this, R.raw.gtaintro); (on all mediaPlayer.create).

Code:

public static class Tab1 extends Fragment {
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.tab1, container, false);



        }
        public Tab1() {
            final ImageButton vitasbut = tab1.findViewById(R.id.vitasbut);

            vitasbut.setOnClickListener(new ImageButton.OnClickListener() {
                @Override
                public void onClick(View v) {
                    vitasbut.setImageResource(R.drawable.vitas1);
                    play(vitasblblblahahah);
                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        public void onCompletion(MediaPlayer mp) {
                            vitasbut.setImageResource(R.drawable.vitas1e2);
                        }
                    });
                }
            });}

        MediaPlayer mediaPlayer;
        private final String gtaintro = "gtaintro";
        private final String gtaagaraga = "gtaagaraga";
        private final String bolsdaqeut = "bolsdaqeut";
        private final String vitasblblblahahah = "vitasblblblahahah";
        private final String vitasuoah = "vitasuoah";
        private final String gabedogbark = "gabedogbark";
        private final String bolsetemqsef = "bolsetemqsef";
        private final String acertomizeravi = "acertomizeravi";
        private final String aiaichoque = "aiaichoque";
        private final String bnsetimaarte = "bnsetimaarte";
        private final String fausterou = "fausterou";
        private final String fausttapegfog = "fausttapegfog";

        private void play(String theText) {
            if (mediaPlayer != null) {
                mediaPlayer.reset();
                mediaPlayer.release();
                mediaPlayer = null;
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                if (Objects.equals(theText, gtaintro))
                    mediaPlayer = MediaPlayer.create(this, R.raw.gtaintro);

                else if (Objects.equals(theText, gtaagaraga))
                    mediaPlayer = MediaPlayer.create(this, R.raw.gtaagaraga);

                else if (Objects.equals(theText, bolsdaqeut))
                    mediaPlayer = MediaPlayer.create(this, R.raw.bolsdaqeut);

                else if (Objects.equals(theText, vitasblblblahahah))
                    mediaPlayer = MediaPlayer.create(this, R.raw.vitasblblblahahah);

                else if (Objects.equals(theText, vitasuoah))
                    mediaPlayer = MediaPlayer.create(this, R.raw.vitasuoah);

                else if (Objects.equals(theText, gabedogbark))
                    mediaPlayer = MediaPlayer.create(this, R.raw.gabedogbark);

                else if (Objects.equals(theText, bolsdaqeut))
                    mediaPlayer = MediaPlayer.create(this, R.raw.bolsdaqeut);

                else if (Objects.equals(theText, bolsetemqsef))
                    mediaPlayer = MediaPlayer.create(this, R.raw.bolsetemqsef);

                else if (Objects.equals(theText, acertomizeravi))
                    mediaPlayer = MediaPlayer.create(this, R.raw.acertomizeravi);

                else if (Objects.equals(theText, aiaichoque))
                    mediaPlayer = MediaPlayer.create(this, R.raw.aiaichoque);

                else if (Objects.equals(theText, bnsetimaarte))
                    mediaPlayer = MediaPlayer.create(this, R.raw.bnsetimaarte);

                else if (Objects.equals(theText, fausterou))
                    mediaPlayer = MediaPlayer.create(this, R.raw.fausterou);

                else if (Objects.equals(theText, fausttapegfog))
                    mediaPlayer = MediaPlayer.create(this, R.raw.fausttapegfog);
            }
            assert mediaPlayer != null;
            mediaPlayer.start();
        }

    }
  • 1

    Try -> final Imagebutton vitasbut = (Imagebutton) getActivity(). findViewById(R.id.vitasbut);

  • It worked. I put it just above public Tab1() { etc etc etc }. Thank you. = ) Any idea about the problem of the create method? I think I need to replace "this", but I don’t know by what. (I saw some answers by stack overflow)

  • You already tried to clean the project?

  • No. I just did and it stayed the same. :/

  • 1

    Okay... try to replace the this for getActivity().getContext()

  • 1

    Also posted on Stackoverflow EN

  • I managed to run it, but crashed the app saying Attempt to invoke virtual method 'android.view.View android.support.v4.app.Fragmentactivity.findViewById(int)' on a null Object Reference in the final line Imagebutton vitasbut = getActivity(). findViewById(R.id.vitasbut); Hi Rosario! = ) What I posted there was to know how to run the code inside Ragment; here I am posting to correct other errors. I thought if I posted the mistakes in that same post, it would give some problem.

Show 2 more comments

1 answer

2


The Fragment works in a different way than a Activity:

In the construction of Activity, we use the method onCreate(), where we inform which xml we will use through the method setContentView(android.view.View).

Already in the construction of Fragment, we use the method onCreateView(), where we return to View screen.

Because of this, Fragment does not have the method findViewById and yes to View (returned in the method onCreateView()).

Try it this way:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.tab1, container, false);
     final ImageButton vitasbut = view.findViewById(R.id.vitasbut);
     // Seu código com o botão vitasbut...
    return view;
}

I do not recommend using the Fragment to initialize the objects on screen, as it is invoked before Build screen (call the method onCreateView()).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.