Skip to content Skip to sidebar Skip to footer

Passing Data From Service To Fragment Giving Null Point

I am trying to get the data from service using BroadcastReceiver and i am able to display that data in the Log but when i try to send the data using the Bundle like this @Override

Solution 1:

see if your getArguments is not null first

if(getArguments() !=null ) // then do the stuff you're doing i.e. pull strings from bundle.

Solution 2:

Instead of passing data indirectly i used the direct way and i set like this in service

Intentintent=newIntent();
    intent.setAction(MY_ACTION);

    intent.putExtra("DATAPASSED", activeAudio.getTitle());
    intent.putExtra("ALBUM_DATA",activeAudio.getAlbum());
    sendBroadcast(intent);

in my fragment

@OverridepublicvoidonStart() {
    super.onStart();


    myReceiver = newMyReceiver();
    IntentFilterintentFilter=newIntentFilter();
    intentFilter.addAction(MediaService.MY_ACTION);
    getActivity().registerReceiver(myReceiver, intentFilter);

}

privateclassMyReceiverextendsBroadcastReceiver {

    @OverridepublicvoidonReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stubStringdatapa= arg1.getStringExtra("DATAPASSED");
        Stringdatap2= arg1.getStringExtra("ALBUM_DATA");
        Log.d("TITLE OF SONG", datapa);
        Log.d("ALBUM", datap2);
        textView.setText(datapa);
        textView1.setText(datap2);
    }

}

Hope this will help some one.

Post a Comment for "Passing Data From Service To Fragment Giving Null Point"