所以我试图从另一个类更新textview。但是,每当我调用该方法时,textview似乎都为null?
我不知道为什么会这样。
以下是相关代码
MediaPlayerService:
@Override
public void onPrepared(MediaPlayer mp) {
UI = new MediaPlayerUI();
mp.start();
int minutesDuration = mp.getDuration() / 6000;
int secondsDuration = (mp.getDuration() % 6000) / 1000;
UI.setDurationText(minutesDuration,secondsDuration);
UI.setProgressBarVisibility(0);
UI.setPauseButtonVisibility(1);
UI.playback();
}
MediaPlayerUI:
public class MediaPlayerUI extends MediaPlayerFragment {
public MediaPlayerUI(){
}
public void setSeekBarProgress(int position){
seekBar.setProgress(position);
}
public void setSeekBarMax(int max){
seekBar.setMax(max);
}
public int getSeekBarProgress(){
return seekBar.getProgress();
}
public void setDurationText(int minutes, int seconds){
}
public void setProgressBarVisibility(int code){
if(code == 0){
progressBar.setVisibility(ProgressBar.GONE);
}else{
progressBar.setVisibility(ProgressBar.VISIBLE);
}
}
public void setPauseButtonVisibility(int code){
if(code == 0){
pauseButton.setVisibility(View.GONE);
}else{
pauseButton.setVisibility(View.VISIBLE);
}
}
MediaPlayerFragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_media_player, container, false);
mSeriesImage = (ImageView) v.findViewById(R.id.series_image);
progressBar = (ProgressBar) v.findViewById(R.id.progress_bar);
seekBar = (SeekBar) v.findViewById(R.id.seek_bar);
playButton = (ImageButton) v.findViewById(R.id.play_button);
pauseButton = (ImageButton) v.findViewById(R.id.pause_button);
messageTitle = (TextView) v.findViewById(R.id.message_title);
seriesTitle = (TextView) v.findViewById(R.id.series_name);
mProgressText = (TextView) v.findViewById(R.id.progress_text);
mDurationText = (TextView) v.findViewById(R.id.duration_text);
mNoAudioText = (TextView) v.findViewById(R.id.no_audio_text);
mNoAudioText.setVisibility(v.GONE);
playButton.setVisibility(v.GONE);
pauseButton.setVisibility(v.GONE);
mAudioURL = getArguments().getString(Keys.AUDIO_URL_KEY);
mModel = getArguments().getParcelable(Keys.INTENT_SERIES_MODEL_OBJECT_KEY);
mPosition = getArguments().getInt(Keys.POSTION_KEY);
messageTitle.setText(mModel.getmMessageTitle().get(mPosition));
seriesTitle.setText(mModel.getmSeriesName());
Intent intent = new Intent(getActivity(), MediaPlayerService.class);
intent.putExtra(Keys.INTENT_SERIES_MODEL_OBJECT_KEY,mModel);
intent.putExtra(Keys.POSTION_KEY,mPosition);
intent.putExtra(Keys.AUDIO_URL_KEY,mAudioURL);
getActivity().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
seekBar.setEnabled(true);
return v;
}
public void setDurationTextView(int minutes,int seconds){
Log.i(TAG,"mDurationText...."+mDurationText);
}
日志输出:
10-29 20:25:00.091 26349-26349/com.tubbs.citychurchob I/MediaPlayerFragment: mDurationText....null
此外,如果我直接在MediaPlayerFragment中调用该方法,则该方法不会返回空值。
我真的只是在黑暗中拍摄如何从服务更新用户界面,因此任何有用的建议都会很棒。谢谢。
答案 0 :(得分:1)
因为当您调用setDurationText()时,无法保证已经创建了TextView。
在这种情况下,您可以在片段中注册广播接收器,然后从服务发送广播
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("update_media_ui");
updateMediaUiReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get the data here and then set the text if
// the TextView is not null or save the data first
// and set it later if the TextView is null.
}
};
registerReceiver(updateMediaUiReceiver, intentFilter);
然后你还需要在Fragment onDestroy()
中取消注册接收器unregisterReceiver(updateMediaUiReceiver);
这是如何从服务发送广播
Intent serviceIntent = new Intent();
serviceIntent.setAction("update_media_ui");
this.sendBroadcast(serviceIntent);