从移动设备发送arraylist< >数据项

t8e9dugd  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(304)

目前我的问题是,每当我试图访问通过dataitem层发送的arraylist时,它都会告诉我对象为null。
有人能解释一下问题的症结所在吗?
可移动的

public class MainActivity extends AppCompatActivity implements  GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

--variables

@Override
protected void onCreate(Bundle savedInstanceState) {

    if(!PreferenceManager.getDefaultSharedPreferences(this)
            .getStringSet("wristaroo", new HashSet<String>()).isEmpty()) {
        tasksSet = PreferenceManager.getDefaultSharedPreferences(this)
                .getStringSet("wristaroo", new HashSet<String>());
        actsList = new ArrayList<>(tasksSet);
    }
    -----

    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Wearable.API)
            .build();

    googleApiClient.connect();

    super.onCreate(savedInstanceState);
    -----
    Button btnSave = (Button) findViewById(R.id.sendButton);
    btnSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            PutDataMapRequest dataMap = PutDataMapRequest.create("/wristaroo/customschedule");
            dataMap.getDataMap().putStringArrayList("schedule", actsList);
            PutDataRequest request = dataMap.asPutDataRequest();

            PendingResult<DataApi.DataItemResult> result = Wearable.DataApi
                    .putDataItem((googleApiClient), request);
            System.out.println("Request: " + request);
            System.out.println("Result: " + result);

        }
    });
}

-----

@Override
public void onConnected(Bundle bundle) {

    new DataTask (this, actsList).execute();
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

class DataTask extends AsyncTask<Node, Void, Void> {

    private ArrayList<String> contents;
    Context c;

    public DataTask ( Context c, ArrayList<String> contents) {
        this.c = c;
        this.contents = contents;
    }

    @Override
    protected Void doInBackground(Node... nodes) {
        PutDataMapRequest dataMap = PutDataMapRequest.create("/wristaroo/customschedule").setUrgent();
        dataMap.getDataMap().putStringArrayList("schedule", actsList);

        PutDataRequest request = dataMap.asPutDataRequest().setUrgent();
        Log.i("Data", "data");
        DataApi.DataItemResult dataItemResult = Wearable.DataApi
                .putDataItem((googleApiClient), request).await();
        System.out.println("dataItemResult: " + dataItemResult);

        return null;
    }
}
}

穿

public class MainActivity extends Activity implements DataApi.DataListener,GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    super.onCreate(savedInstanceState);

    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Wearable.API)
            .build();
---------

@Override
public void onConnected(Bundle bundle) {
    Wearable.DataApi.addListener(googleApiClient, this).setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(@NonNull Status status) {
            Log.i("myTag", String.valueOf(status));
        }
    });
    System.out.println("onConnected Activated!!!");
}

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    for (DataEvent event : dataEvents) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            // DataItem changed
            DataItem item = event.getDataItem();
            if (item.getUri().getPath().compareTo("/wristaroo/customschedule") == 0) {
                DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
                data = dataMap.getStringArrayList("schedule");
            }
        } else if (event.getType() == DataEvent.TYPE_DELETED) {
            // DataItem deleted
        }
    }
}

@Override
public void onConnectionSuspended(int i) {
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)

舱单移动

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.michaeldvinci.conedmiro.schedaroo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".EditActivity"
            android:label="@string/app_name">
        </activity>
        <activity
            android:name=".AddActivity"
            android:label="@string/app_name">
        </activity>
    </application>

</manifest>

明显磨损

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.michaeldvinci.conedmiro.schedaroo">

    <uses-feature android:name="android.hardware.type.watch" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.DeviceDefault">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".byDayActivity"
            android:label="@string/app_name">
        </activity>
        <activity
            android:name=".byChoiceActivity"
            android:label="@string/app_name">
        </activity>
        <activity
            android:name=".byStageActivity"
            android:label="@string/app_name">
        </activity>
        <activity
            android:name=".byTimeActivity"
            android:label="@string/app_name">
        </activity>
        <activity
            android:name=".byStageActActivity"
            android:label="@string/app_name">
        </activity>
    </application>

</manifest>

当我按下移动应用程序上的[发送]按钮时输出

03-29 18:33:47.948 5246-5246/com.michaeldvinci.conedmiro.schedaroo W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=1307.8564, y[0]=2230.7813, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=473778, downTime=468682, deviceId=0, source=0x1002 }
03-29 18:33:47.948 5246-5246/com.michaeldvinci.conedmiro.schedaroo W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=1307.8564, y[0]=2230.7813, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=473778, downTime=468682, deviceId=0, source=0x1002 }
03-29 18:33:47.948 5246-5246/com.michaeldvinci.conedmiro.schedaroo W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=1307.8564, y[0]=2230.7813, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=473778, downTime=468682, deviceId=0, source=0x1002 }
03-29 18:33:47.948 5246-5246/com.michaeldvinci.conedmiro.schedaroo W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=1307.8564, y[0]=2230.7813, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=473778, downTime=468682, deviceId=0, source=0x1002 }
03-29 18:33:48.179 5246-5246/com.michaeldvinci.conedmiro.schedaroo I/System.out: Request: PutDataRequest[dataSz=108, numAssets=0, uri=wear:/wristaroo/customschedule, syncDeadline=0]
03-29 18:33:48.179 5246-5246/com.michaeldvinci.conedmiro.schedaroo I/System.out: Result: com.google.android.gms.wearable.internal.zzx$1@1ded1a8
bwitn5fc

bwitn5fc1#

这可能最终是一个错误的答案,但我认为 setUrgent() 标志可能很重要:
请使用此代码:

PutDataMapRequest dataMap = PutDataMapRequest.create("/wristaroo/customschedule");
    dataMap.getDataMap().putStringArrayList("schedule", actsList);

    PutDataRequest request = dataMap.asPutDataRequest();
    request.setUrgent();
    Log.i("Data", "data");
    DataApi.DataItemResult dataItemResult = Wearable.DataApi
            .putDataItem((googleApiClient), request).await();

相关问题