我想将integer
值从Activity
Class( PlayListActivity.java
) 传递给我的Service
( AudioService.java
) 类。但是,当我单击 时ListItem
,我的应用程序会停止。我想不出错误。希望有人可以帮助我
logCat 中的错误:
java.lang.RunTimeException: error recieveing broadcast Intent {act-com.exaple.serviceaudio.AudioService.PLAY_SONG flg=0x10 (has extras)} in com.example.serviceaudio.AudioService$AudioPlayerBroadcastReciever@41b4e818.........
这是我的代码。
PlayLlistActivity.java
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
playSongSong(position);
}});
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
unbindService(serviceConnection);
super.onDestroy();
}
public void playSongSong(int songIndex){
Intent intent = new Intent(AudioService.PLAY_SONG);
intent.putExtra("songIndex", songIndex);
this.sendBroadcast(intent);
}
音频服务.java
public static final String PLAY_SONG_BASE = "com.example.serviceaudio.AudioService";
public static final String PLAY_SONG = PLAY_SONG_BASE + ".PLAY_SONG";
private AudioPlayerBroadcastReceiver broadcastReceiver = new AudioPlayerBroadcastReceiver();
@Override
public void onCreate() {
Log.v("AudioService", "AudioPlayer: onCreate() called");
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(PLAY_SONG);
registerReceiver(broadcastReceiver, intentFilter);
}
public void onDestroy() {
unregisterReceiver(broadcastReceiver);
}
private class AudioPlayerBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
int currentSongIndex = intent.getExtras().getInt("songIndex");
if(PLAY_SONG.equals(action)) {
Log.d("AudioService", "Action not recognized: " + action);
playSong(currentSongIndex);
}}
当我评论int currentSongIndex = intent.getExtras().getInt("songIndex");
和`playSong(currentSongIndex); 没有错误,但什么也不做。