片段和服务器之间的通信问题
Communication problems between fragment and a server
我有一个片段想与服务器通信,但问题是我总是得到空指针
我的 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");
//}
}
}
我遇到了这个致命异常: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
希望你能帮帮我
您不能像 class 那样在 Android 中创建 MusicService。它是一项服务,必须以正确的方式启动https://medium.com/google-developers/mediabrowserservicecompat-and-the-modern-media-playback-app-7959a5196d90
在MusicService中,我添加了这个方法
public void pausePlay() {
PauseRequest();
}
然后在BrowseFragment中,在onCreateView()
Button button = (Button)rootView.findViewById(R.id.button_play_pause);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mMusicService.pausePlay();
}
});
我有一个片段想与服务器通信,但问题是我总是得到空指针
我的 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");
//}
}
}
我遇到了这个致命异常: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
希望你能帮帮我
您不能像 class 那样在 Android 中创建 MusicService。它是一项服务,必须以正确的方式启动https://medium.com/google-developers/mediabrowserservicecompat-and-the-modern-media-playback-app-7959a5196d90
在MusicService中,我添加了这个方法
public void pausePlay() {
PauseRequest();
}
然后在BrowseFragment中,在onCreateView()
Button button = (Button)rootView.findViewById(R.id.button_play_pause);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mMusicService.pausePlay();
}
});