Reputation: 937
So I'm trying to update a textview from another class. However,whenever I'm calling the method, It seems the textview is null?
I have no idea why that would be the case.
Here is the relevant code
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);
}
Log Output:
10-29 20:25:00.091 26349-26349/com.tubbs.citychurchob I/MediaPlayerFragment: mDurationText....null
Also, if I call the method directly inside MediaPlayerFragment, the method does NOT return a null value.
I'm really just shooting in the dark on how to update the UI from a Service so any helpful advice on that would be great also. Thank you.
Upvotes: 1
Views: 74
Reputation: 11112
Because it's not guaranteed that the TextView is already created or not when you are calling setDurationText().
In that case you can register a broadcast receiver in your fragment and then send broadcast from the service
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);
and then you also need to unregister the receiver in Fragment onDestroy()
unregisterReceiver(updateMediaUiReceiver);
This is how to send broadcast from service
Intent serviceIntent = new Intent();
serviceIntent.setAction("update_media_ui");
this.sendBroadcast(serviceIntent);
Upvotes: 1