如何正确实例化 DataClient,以便在我尝试发送 DataItem 时它不为 NULL?

How to properly instantiate DataClient so that it is not NULL when I attempt to send DataItem?

我正在尝试通过 DataLayer 将数据从我的可穿戴设备发送到我的 phone。 mobile的代码还没写完,是不是要先写完再从wearable传东西到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) 正在被另一个 class 调用,这应该没有问题,因为我有调试日志确认它正在工作。

我尝试使用 GoogleApiClient.Builder(this).addApi(Wearable.API) 并通过 Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq) 发送内容,但当我这样做时,即使是 mGoogleApiClient returns 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);
                }
            });


}
}

感觉我在代码中遗漏了一些非常简单的东西,但我不知道它是什么。任何帮助将不胜感激。

问题是在我的 class 中调用 sendData() 这样做:

WearActivity mAct = new WearActivity();
mAct.sendData();

根据给出的答案 here,我将其更改为将 activity 作为参数发送,方法是将其添加到 class 构造函数中,如下所示:

// in WearActivity
MyClass instanceClass = new MyClass(this);

// in MyClass
public class MyClass{
    private WearActivity mAct;
    public MyClass (WearActivity activity) {
        mAct = activity;
    }

// code
mAct.sendData();