我已经开始为Android Wear创建Watch Face。我已经实现了几乎所有的东西,现在我想在脸上显示手机电池。根据我在研究后的理解,这只能通过Message或Data Layer API实现。所以我开始在他所在地区工作,但我一开始就遇到了问题。我无法连接到Google Api客户端。
我有两个课程"穿" - 手表(服务)的一个通用和我创建的通用:
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();
}
我只是像这样实例:BatteryActivity batteryActivity = new BatteryActivity();
我收到这个错误:
java.lang.NullPointerException:尝试调用虚方法 ' android.os.Looper android.content.Context.getMainLooper()'在null 对象参考
如果我实例化的话,我不明白mGoogleApiClient是如何为空的。 作为一个注释 - 这纯粹基于谷歌文档 - https://developer.android.com/training/wearables/data-layer/messages.html
答案 0 :(得分:7)
我想你应该在onCreate
方法上构建GoogleApiClient。你的代码是这样的:
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();
}
}
答案 1 :(得分:1)
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();
答案 2 :(得分:0)
理想情况下,这应该通过异步任务完成:https://developer.android.com/google/auth/api-client.html#Communicating
private class WatchTask extends AsyncTask<String, Void, Void> {
protected void doInBackground(String text) {
或者为了快速解决问题,您可能只是在错误的位置使用它:
在onCreate
使用getActivity()
或this
中使用Context
代替此行:
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
我还建议退房:http://developer.android.com/training/wearables/data-layer/index.html
如果你真的想要使用某个活动,那么它看起来像是:
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) { }
}