获取Service对象上的空对象错误。此方法未初始化。
private ServiceConnection musicConnection = new ServiceConnection()
它的containg选项卡主机然后是这个Fragment。
public class PrimaryFragment extends Fragment {
RecyclerView rv;
LazyAdapter adapter;
ListView listView;
View rootView;
private MusicService serviceMusic;
ArrayList<AudioListModel> songsList;
private Intent playIntent;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.primary_layout, container, false);
songsList = SongsManager.GetSongs();
adapter = new LazyAdapter(getActivity(), songsList.toArray(new AudioListModel[songsList.size()]));
//rv = (RecyclerView) rootView.findViewById(R.id.songs_recycleview);
listView = (ListView) rootView.findViewById(R.id.SongList);
LazyAdapter ad = new LazyAdapter(getActivity(), songsList.toArray(new AudioListModel[songsList.size()]));
listView.setItemsCanFocus(false);
listView.setAdapter(ad);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(getActivity(), position + "this is on click event", Toast.LENGTH_SHORT).show();
serviceMusic.setSelectedSong(position, MusicService.NOTIFICATION_ID); // getting error here......
Intent i = new Intent(getActivity(), PlaySongActivity.class);
startActivity(i);
}
});
return rootView;
}
// This method is not initializing.
private ServiceConnection musicConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MusicService.PlayerBinder binder = (MusicService.PlayerBinder) service;
//get servic1
serviceMusic = binder.getService();
serviceMusic.setSongList(songsList);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
public void onStart() {
super.onStart();
//Start service
//Toast.makeText(getActivity(), "before onStart", Toast.LENGTH_SHORT).show();
if (playIntent == null) {
//Toast.makeText(getActivity(), "after onStart", Toast.LENGTH_SHORT).show();
playIntent = new Intent(getActivity(), MusicService.class);
getActivity().bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
getActivity().startService(playIntent);
}
}
}这是音乐服务班
public class MusicService extends Service implements
MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener,
MediaPlayer.OnCompletionListener {
private MediaPlayer mPlayer;
private Uri mSongUri;
private ArrayList<ListModel> mListSongs;
private int SONG_POS = 0;
private final IBinder musicBind = new PlayerBinder();
private Notification.Builder notificationBuilder;
private Notification mNotification;
public class PlayerBinder extends Binder {//Service connection to play in background
public MusicService getService() {
Log.d("test", "getService()");
return MusicService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
Log.d("test", "onBind Called ");
return musicBind;
}发布于 2016-10-02 14:56:46
我没有在“MusicService”中注册"AndroidManifest.xml“。所以我把这条线包括进去,它就开始起作用了。"< service android:name=".MusicService“/>”我们需要在清单中注册服务。
https://stackoverflow.com/questions/39817040
复制相似问题