0
在Service
对象上获得空对象错误。此方法不是初始化ServiceConnection onServiceConnected未调用
private ServiceConnection musicConnection = new ServiceConnection()
其方含接头主机那么这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;
}
on onStart()?我也尝试过。删除playIntent == null。仍然低于错误。 “尝试调用空对象引用虚拟方法'void com.tabproject.MusicService.setSelectedSong(int,int)' –
你能帮助我解决代码... @tynn感谢advnc –
你是如何实现'MusicService' ?它看起来像活页夹没有正确实施。 – tynn