我有一个片段,我想与服务器通信,但问题是我总是得到空指针
我在我的片段 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
}
});
}
}
这是我的服务:
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");
//}
}
}
我得到了这个致命的例外:主要
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
我希望你能帮帮我