Call non-static methods from static inner classes… here is a solution to the problem.
Call non-static methods from static inner classes
I’m new to Android and I’m having this issue with nullPointerException when I call a non-static method from a static inner class, below is my code.
public void playPauseMusic() {
check for already playing
if (mp.isPlaying()) {
if (mp != null) {
mp.pause();
Changing button image to play button
btnPlay.setImageResource(R.drawable.btn_play);
}
} else {
Resume surah
if (mp != null) {
mp.start();
Changing button image to pause button
btnPlay.setImageResource(R.drawable.btn_pause);
}
}
}
public static class notifyPlayPauseListner extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("PLAY/Pause Tag","In the listener Play/Pause ");
MainActivity mc = new MainActivity();
mc.playPauseMusic();
}
}
Probably a simple concept, but I’m new to android, that’s why I asked. Please help
Solution
MainActivity mc = new MainActivity();
There is no point in instantiating a new activity. Instead, you need to find an existing instance of MainActivity
and call the methods it wants.
Also, using activities to play music is not a good idea. Consider using service