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();
}
}