Java – Bind service error

Bind service error… here is a solution to the problem.

Bind service error

I

bind (bind) to a service in 3 activities and in 2 activities it works fine, but in activity 3 I get an error.

This 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();
    }
}

This is my activity where 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;
        }
    };
}

I have a bug :

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

Solution

I don’t know why it works with the other 2 activities, but in this case you naturally 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 (bind) to the service, which is done in onStart, but it is used in onCreate and onCreate always runs before onStart, which produces a NullPointerException.

mBound is useful if you want to ensure that clientBluetoothService is initialized when you use it.

Related Problems and Solutions