我正在尝试通过DataLayer将数据从可穿戴设备发送到手机。用于移动设备的代码尚未完成,在我将数据从可穿戴设备发送到DataLayer之前,我是否必须完成编写工作?
现在,当我尝试执行mDataClient.putDataItem(putDataReq)
时,出现以下错误:java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.android.gms.wearable.DataClient.putDataItem(com.google.android.gms.wearable.PutDataRequest)' on a null object reference
sendData(data)
被另一个类调用,这应该没有问题,因为我有调试日志确认它正在工作。
我尝试使用GoogleApiClient.Builder(this).addApi(Wearable.API)
并通过Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq)
发送邮件,但是当我这样做时,甚至mGoogleApiClient都返回null。
这是我的build.gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.watchsleep"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.android.support:wearable:2.4.0'
implementation 'com.google.android.gms:play-services-wearable:16.0.1'
implementation 'com.android.support:percent:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:wear:28.0.0'
compileOnly 'com.google.android.wearable:wearable:2.4.0'
}
这是我的代码:
public class WearActivity extends WearableActivity implements DataClient.OnDataChangedListener {
private static final String COUNT_KEY = "com.example.count";
private DataClient mDataClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wear);
// Enables Always-on
setAmbientEnabled();
// initialise API client for sending data to phone here
mDataClient = Wearable.getDataClient(this);
}
@Override
protected void onStart(){
super.onStart();
Log.d(TAG, "onStart");
mDataClient = Wearable.getDataClient(this);
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
mDataClient = Wearable.getDataClient(this);
Wearable.getDataClient(this).addListener(this);
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
Wearable.getDataClient(this).removeListener(this);
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_DELETED) {
Log.d(TAG, "DataItem deleted: " + event.getDataItem().getUri());
} else if (event.getType() == DataEvent.TYPE_CHANGED) {
Log.d(TAG, "DataItem changed: " + event.getDataItem().getUri());
}
}
}
// Create a data map and put data in it
public void sendData(ArrayList<String> data) {
Log.d(TAG, "sending data");
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count"); // create data map
putDataMapReq.getDataMap().putStringArrayList(COUNT_KEY, data); // put data in map
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
Task<DataItem> putDataTask = mDataClient.putDataItem(putDataReq); // ERROR COMES FROM THIS LINE
putDataTask.addOnSuccessListener(
new OnSuccessListener<DataItem>() {
@Override
public void onSuccess(DataItem dataItem) {
Log.d(TAG, "Sending text was successful: " + dataItem);
}
});
}
}
感觉好像我在代码中丢失了一些非常简单的东西,但是我不知道它是什么。任何帮助将不胜感激。
答案 0 :(得分:0)
问题是我在课堂上通过以下方式调用sendData()
:
WearActivity mAct = new WearActivity();
mAct.sendData();
按照给出的答案here,我将其更改为将活动作为参数发送,方法是将其添加到类构造函数中,如下所示:
// in WearActivity
MyClass instanceClass = new MyClass(this);
// in MyClass
public class MyClass{
private WearActivity mAct;
public MyClass (WearActivity activity) {
mAct = activity;
}
// code
mAct.sendData();