Dinamo
Dinamo

Reputation: 61

Error when sending data from Service to Activity

I'm trying to send data from Service to Activity, Here is my code currently:

interface sListener {
    void OnNotify      (String data);
}

public class MainActivity extends AppCompatActivity implements ServiceConnection, sListener {
    sService service;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindService(new Intent(this, sService.class), this, Context.BIND_AUTO_CREATE);

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

    }
    @Override
    public void onStop() {
        if(service != null && !isChangingConfigurations())
            service.detach();
        super.onStop();
    }
    
    public void OnNotify(String data){
        //... get data from Service
    }
    
    @Override
    public void onServiceConnected(ComponentName name, IBinder binder) {
        System.out.println("onServiceConnected");
        service = ((sService.sBinder) binder).getService();
        service.attach(this);
        startService(new Intent(this, sService.class));
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        service = null;
    }
    
}




public class sService extends Service {
    sReceiver receiver = new sReceiver();
    private final IBinder binder;
    
    public sService(){
        binder = new sBinder();
    }
    
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return binder;
    }
    
    class sBinder extends Binder {
        sService getService() { return sService.this; }
    }
    
    public void attach(sListener listener) {
        Log.d("sService","attach()");
        if(Looper.getMainLooper().getThread() != Thread.currentThread())
            throw new IllegalArgumentException("not in main thread");

        synchronized (this) {
            receiver.setListener(listener);
            //this.listener = listener;
        }


    }
    
    public void detach() {
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        registerReceiver(receiver, new IntentFilter("android.intent.action.BATTERY_LOW"));

        return START_STICKY;
    }
    
    static public class sReceiver extends BroadcastReceiver implements sListener {
        sListener listener;
        
        public void setListener(sListener listener){
            this.listener = listener;
        }
        
        public void OnNotify(String data){
            listener.OnNotify(data); //send data to Activity
        }
        
        
    }
}

I get the following error when OnNotify is invoked:

java.lang.RuntimeException: Unable to start receiver com.example.sService$sReceiver: java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.sListener.OnNotify(java.lang.String)' on a null object reference

Why I'm receiving this error?

Thanks.

Upvotes: 0

Views: 19

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006974

listener.OnNotify(data); //send data to Activity

Check to see if listener is null before trying to call OnNotify(). Or, do not call registerReceiver() until you set the listener in attach(). My guess is that you are receiving a broadcast before attach() is called.

Upvotes: 1

Related Questions