1

I have a fragmment that I would like to communicate with a server, but the problems is that i always get null pointer

I have this in my fragment onCreateView:

public class BrowseFragment extends Fragment implements View.OnClickListener{

private MusicService mMusicService;


@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment, container, false);



    mMusicService = new MusicService(this);
             mMusicService.setCallback(new MusicService.Callback() {


                                @Override
                                public void onPlayPause(String state) {
                                    //Do something
                                }
                            });

}
}

This is my service:

 public class MusicService extends MediaBrowserServiceCompat {


public interface Callback {
        public void onPlayPause(String state);
    }

    public void setCallback(Callback callback) {
        this.mCallback = callback;
    }





private void PauseRequest() {
        mPlayback.pause();
        //if (mCallback != null) {
            mCallback.onPlayPause("PAUSE");
        //}
}

}

I´m getting this FATAL EXCEPTION: main

Process: com.geobytenet.mediaplayer, PID: 3262
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.android.mediabrowserservice.MusicService$Callback.onPlayPause(java.lang.String)' on a null object reference

I hope you can help me

  • https://stackoverflow.com/questions/39179317/mediabrowserservicecompat-unable-to-bind-to-service – akshay_shahane Jul 28 '17 at 19:10
  • Maybe i wans't clear. The server is working what is not working is the interface and im getting an error in mCallback.onPlayPause("PAUSE"); – Gerardo Mendez Dot Jul 28 '17 at 19:59
  • Is your fragment being displayed when you try to use the interface? If it no longer has focus, there's a chance your fragment is being destroyed before you call the implementation. Are you sure that `mCallback` doesn't get altered elsewhere in the Service? I've put together something similar & this works for me. – Gary99 Jul 29 '17 at 18:06
  • Yes my fragment is being displayed, the app is working fine, but i get the error when i click on the button that do the CallBack. Can you share the code you said your working to see if it helps me? – Gerardo Mendez Dot Jul 29 '17 at 19:18
  • Mostly, I just copied the code you posted. Since you didn't post how you call `PauseRequest()`, I wasn't sure how it was called. I created a public method in the Service & call `PauseRequest()` from it. Then in the fragment, I created a button & in `onClick()`, I call the public method in the Service `mMusicService.pausePlay();` If the formatting due to this being in a comment makes it unreadable, I can post an answer if you need. – Gary99 Jul 29 '17 at 19:44
  • Sure please do that so I can accept your answerd, Thank you – Gerardo Mendez Dot Jul 31 '17 at 11:47
  • I just posted the info as an answer. Sorry for the delay. I had this marked as a Favorite but SO never notified me of your comment. – Gary99 Aug 03 '17 at 20:35

2 Answers2

0

You can not create MusicService in Android like simple class. It is a service and it must be started in a right way https://medium.com/google-developers/mediabrowserservicecompat-and-the-modern-media-playback-app-7959a5196d90

HooliGun
  • 46
  • 3
  • Maybe i wans't clear. The server is working what is not working is the interface and im getting an error in mCallback.onPlayPause("PAUSE"); – Gerardo Mendez Dot Jul 28 '17 at 20:00
0

In MusicService, I added this method

    public void pausePlay() {
        PauseRequest();
    }

Then in BrowseFragment, in onCreateView()

    Button button = (Button)rootView.findViewById(R.id.button_play_pause);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mMusicService.pausePlay();
        }
    });
Gary99
  • 1,750
  • 1
  • 19
  • 33