I've started creating Watch Face for Android Wear. I've implemented almost everything and now I want to show on the face the phone battery. From what I understood after my research this is only doable via Message or Data Layer API. So I've started working in his area but I'm facing a problem in the very beginning. I cannot connect to Google Api Client.
I have two classes under "wear" - one generic for the watch (service) and one created by me:
public class BatteryActivity extends Activity {
    GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks( new ConnectionCallbacks() {
            @Override
            public void onConnected(Bundle connectionHint) {
                Log.i( "", "onConnected: " + connectionHint);
                // Now you can use the Data Layer API
            }
            @Override
            public void onConnectionSuspended(int cause) {
                Log.i( "", "onConnectionSuspended: " + cause);
            }
        })
        .addOnConnectionFailedListener( new OnConnectionFailedListener() {
            @Override
            public void onConnectionFailed(ConnectionResult result) {
                Log.i( "", "onConnectionFailed: " + result);
            }
        })
                // Request access only to the Wearable API
        .addApi(Wearable.API)
        .build();
}
Whan I simply instance it like this: BatteryActivity batteryActivity = new BatteryActivity();
I get this error: 
java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference
I don't understand how mGoogleApiClient is null if I've instance it. As a note - this is purely based on google documentation here - https://developer.android.com/training/wearables/data-layer/messages.html
I suppose you should build the GoogleApiClient on onCreate method. Your code would be like this:
public class BatteryActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);
        GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks( new ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle connectionHint) {
                    Log.i( "", "onConnected: " + connectionHint);
                    // Now you can use the Data Layer API
                }
                @Override
                public void onConnectionSuspended(int cause) {
                    Log.i( "", "onConnectionSuspended: " + cause);
                }
            })
            .addOnConnectionFailedListener( new OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    Log.i( "", "onConnectionFailed: " + result);
                }
            })
        // Request access only to the Wearable API
        .addApi(Wearable.API)
        .build();
    }
}
                        It also happens when not providing proper context. Pass context like this :
mGoogleApiClient = new GoogleApiClient.Builder(context) // Pass context here
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API).build();
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With