Error putting ImageButtons with sounds within fragment

1

I'm trying to put ImageButtons playing sounds inside a fragment called Tab1, however I'm encountering two errors:

  Non-static method 'findViewById (int)' can not be referenced from a   static context

on the line

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

and

  

Can not resolve method   'create (com.example.mateuspc1.memepocket.MainActivity.Tab1, int)'

lines in mediaPlayer = MediaPlayer.create(this, R.raw.gtaintro); (in 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();
        }

    }
    
asked by anonymous 07.03.2018 / 15:15

1 answer

2

Fragment works differently than Activity :

When building Activity, we use the onCreate () method, where we inform you what xml we will use through the setContentView (android.view.View) .

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

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

Try this:

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 constructor to initialize the objects on the screen, since it is invoked before Constructing the screen (call the onCreateView() method).

    
16.03.2018 / 16:32