Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bindservice error

I'm binding to a service in 3 activities, in 2 it works well but in 3rd i've got an error.

Here is my service:

public class ClientBluetoothService extends Service {
    private Handler handler = new Handler();

    private final IBinder mBinder = new LocalBinder();

    private ClientBluetooth clientBT;

    public class LocalBinder extends Binder {
        ClientBluetoothService getSerivce() {
            return ClientBluetoothService.this;
        }
    }
    public ClientBluetoothService() {
        clientBT = new ClientBluetooth();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public void generateToast() {
        Log.d("HERE", "Service: generateToast()");
        Toast.makeText(getApplicationContext(), "WITAJ W SERWISIE", Toast.LENGTH_SHORT).show();
    }

    public void newClient() {
        clientBT = new ClientBluetooth();
    }

    public void start() {
        clientBT.start();
    }

    public String getStatus() {
        return clientBT.getStatus();
    }

    public void disconnect() throws IOException {
        clientBT.disconnect();
    }
}

And here is my activity in which I have an error:

public class MeasureActivity extends AppCompatActivity {

    protected boolean mBound = false;
    private ClientBluetoothService clientBluetoothService;

    private TextView textView;

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent(this, ClientBluetoothService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        Log.d("HERE", "Measure: onStart()");
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_measure);

        setTitle("Measure");
        textView = (TextView)findViewById(R.id.textView);
        clientBluetoothService.generateToast();
    }

    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {

            ClientBluetoothService.LocalBinder binder = (ClientBluetoothService.LocalBinder) service;
            clientBluetoothService = binder.getSerivce();
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };
}

And I've got an error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.gmail.krakow.michalt.myecg.ClientBluetoothService.generateToast()' on a null object reference

like image 500
michalt38 Avatar asked Feb 24 '26 14:02

michalt38


1 Answers

I don't know why it works with the other 2 activities, however in this case it is natural that you get a NullPointerException. The order in the Activity lifecycle is:

onCreate() -> onStart() -> onResume() -> onPause() -> onStop() -> and onDestroy(). (https://developer.android.com/guide/components/activities/activity-lifecycle.html)

clientBluetoothService is initialized after binding to service which is done done in onStart, however it is used in onCreate and onCreate always runs before onStart which gives NullPointerException.

mBound can be useful if you want to be sure that clientBluetoothService is initialized when you are using it.

like image 192
merterpam Avatar answered Feb 27 '26 03:02

merterpam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!