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

 Intent intent = new Intent();
    intent.setAction(MY_ACTION);

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

in my fragment

 @Override
public void onStart() {
    super.onStart();


    myReceiver = new MyReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(MediaService.MY_ACTION);
    getActivity().registerReceiver(myReceiver, intentFilter);

}

private class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

        String datapa = arg1.getStringExtra("DATAPASSED");
        String datap2 = 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"