Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException when creating GoogleApiClient instance

Tags:

android

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

like image 393
Георги Ангелов Avatar asked Apr 06 '15 23:04

Георги Ангелов


2 Answers

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();
    }
}
like image 132
John Cordeiro Avatar answered Nov 02 '22 23:11

John Cordeiro


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();
like image 1
SANAT Avatar answered Nov 02 '22 23:11

SANAT