ServiceConnection onServiceConnected未调用

时间:2016-10-02 12:32:50

标签: android android-layout android-studio

Service对象上获取空对象错误。此方法未初始化

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;
}

1 个答案:

答案 0 :(得分:1)

我没有在&#34; AndroidManifest.xml&#34;中注册MusicService。所以我包括这一行,它开始工作。       &#34;&LT; service android:name =&#34; .MusicService&#34; /&GT;&#34; 我们需要在清单中注册服务..