0

When I try to execute a method in my service type class that changes an image from another class, the following error occurs:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setImageResourse(int)' on a null object reference

My class of service code:

public class AudioPlayerService extends MediaBrowserServiceCompat implements Player.EventListener {

public void OnCreate() {
super.(onCreate);

changeImage();

}

public void changeImage() {

PlayerActivity playerActivity = new PlayerActivity();
playerActivity.testeImagem();

}

}

Class code to be changed:

    public class PlayerActivity extends AppCompatActivity implements MediaPlayer.OnBufferingUpdateListener,
        MediaPlayer.OnCompletionListener, ExoPlayer.EventListener {

    ImageButton play_pause_image, estrela_favorito, voltar_musica, proxima_musica, info, repeat;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);

        play_pause_image =findViewById(R.id.play_pause_icon);

}

    public void testeImagem() {
       play_pause_image.setImageResource(R.drawable.ic_baseline_play_arrow);
    }

}

The codes are simplified with just the lines necessary for you to help me, thanks.

  • 1
    Hi João. The error indicates setText was called on a TextView that is null, but I do not see any TextViews in the code provided. Regards. – Elletlar Jul 16 '20 at 22:52
  • 1
    hello, actually it is to change the setText for setImageResource, the setText was from a test that I tried to do before, so disregard the setText and consider the setImageResource – João Pedro Souza Jul 16 '20 at 22:56
  • The problem is that the code is creating the Activity itself which should 'never' be done in Android. The framework always creates the Activity for you. But since it was created manually, onCreate was never called which means setContentView was not called and that is why the button is null and the app is crashing. – Elletlar Jul 16 '20 at 23:02
  • @Elletlar Thank you very much for the comment, do you know a way to solve this? – João Pedro Souza Jul 16 '20 at 23:04
  • 1
    I was afraid that you were going to ask that. It depends a little on your architecture. You could join the Activity to the service using something called a "binder". If you just want to send an event from the Service to the Activity traditionally this would have been done with a LocalBroadcastReceiver or perhaps RxJava. But now is usually done with LiveData which is like a software bus for sending messages that also respects the Android lifecycle. – Elletlar Jul 16 '20 at 23:15
  • 1
    [How to have Android Service communicate with Activity](https://stackoverflow.com/questions/2463175/how-to-have-android-service-communicate-with-activity). The answer by zeenosaur is the most modern, for now. – Elletlar Jul 16 '20 at 23:17
  • @Elletlar thank you very much, i will try this – João Pedro Souza Jul 16 '20 at 23:19

0 Answers0