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();
}
}
Try -> final Imagebutton vitasbut = (Imagebutton) getActivity(). findViewById(R.id.vitasbut);
– Edilson Ngulele
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)
– Asdra
You already tried to clean the project?
– Edilson Ngulele
No. I just did and it stayed the same. :/
– Asdra
Okay... try to replace the
this
forgetActivity().getContext()
– Edilson Ngulele
Also posted on Stackoverflow EN
– Rosário Pereira Fernandes
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.
– Asdra