NullPointerException on instance of GoogleApiClient

I started creating Watch Face for Android Wear. I have implemented almost everything, and now I want to show the phone’s battery on my face. From what I understood after my research, this can only be done through the message or data API. Therefore, I began to work in my field, but in the face of problems at the very beginning. I cannot connect to the Google Api client.

I have two classes for "wear and tear" - one common for watches (services) 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(); } 

What I just repeat like this: BatteryActivity batteryActivity = new BatteryActivity(); I get this error:

java.lang.NullPointerException: attempt to call the virtual method 'android.os.Looper android.content.Context.getMainLooper ()' at zero object reference

I do not understand how mGoogleApiClient is null if I instantiate it. As a side note - this is purely based on Google documentation here - https://developer.android.com/training/wearables/data-layer/messages.html

+6
source share
3 answers

I assume that you should build GoogleApiClient using the onCreate method. Your code will look 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(); } } 
+6
source

This also happens when the proper context is not provided. Skip the context as follows:

 mGoogleApiClient = new GoogleApiClient.Builder(context) // Pass context here .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API).build(); 
+1
source

Ideally, this should be done using the Async task: https://developer.android.com/google/auth/api-client.html#Communicating

 private class WatchTask extends AsyncTask<String, Void, Void> { protected void doInBackground(String text) { 

Or for a quick fix, you can simply use this in the wrong place:

In onCreate use getActivity() or this to use Context for this line:

 GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this) 

I also suggest checking: http://developer.android.com/training/wearables/data-layer/index.html

If you really want to use activity, then it will look something like this:

 public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, MessageApi.MessageListener { private GoogleApiClient googleClient; // A google client for Wear interaction @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Build a new GoogleApiClient that includes the Wearable API googleClient = new GoogleApiClient.Builder(this) .addApi(Wearable.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); } @Override public void onConnected(Bundle connectionHint) { Wearable.MessageApi.addListener(googleClient, this); } @Override public void onMessageReceived(MessageEvent messageEvent) { //Same onMessageReceived as in WearableListenerService } @Override public void onConnectionFailed(ConnectionResult connectionResult) { } @Override public void onConnectionSuspended(int i) { } } 
0
source

Source: https://habr.com/ru/post/984764/


All Articles