无法从Wear Data层获取数据

时间:2015-04-15 19:52:41

标签: java android wear-os android-wear-data-api

我正在尝试将手持设备中的列表同步到可穿戴设备。在电话方面,我有一个列表视图,你可以添加项目,在磨损方面,我只是显示相同的列表。我正在向/card/id路径添加项目,并在/counter上添加数组大小。当我将项目添加到phoneide上的列表时,会调用OnDataChanged方法,但是当我尝试在wearside上读取项目时,它不起作用。当我试图获取连接的节点时,它会给出nullpointer异常,因此我无法读取数据。这是代码和日志快照:

错误

  

04-15 12:41:38.075:E / AndroidRuntime(13791):引起:java.lang.NullPointerException:尝试调用接口方法'android.os.Looper com.google.android.gms.common.api 。对象空间对象引用上的.GoogleApiClient.getLooper()'

磨损面:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mDefaultCircleRadius = getResources().getDimension(R.dimen.default_settings_circle_radius);
    mSelectedCircleRadius = getResources().getDimension(R.dimen.selected_settings_circle_radius);
    cards = new ArrayList<GiftCard>();
    new LoadCards().execute();

    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            //mTextView = (TextView) stub.findViewById(R.id.count_text);
            mListView = (WearableListView) stub.findViewById(R.id.card_list_view);

        }
    });

    mHandler = new Handler();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

}


@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(COUNT_PATH) == 0) {
                DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
                final String counter = dataMap.getString(CardUtil.CARD_COUNT);
                new LoadCards().execute();

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(c,"Card count: "+ counter,Toast.LENGTH_LONG).show();

                        //mTextView.setText("COUNTER FROM DATACHANGE METHOD: " + counter);
                    }
                });


            }
            else if(item.getUri().getPath().compareTo(CARD_PATH) == 0){

            }
        } else if (event.getType() == DataEvent.TYPE_DELETED) {
            // DataItem deleted
        }
    }
}


public GiftCard loadCardData(int id){
    //Uri uri = getIntent().getData().buildUpon().encodedPath("/card").appendPath(String.valueOf(id)).build();
   Uri uri = getUriForDataItem("/counter");
    Log.d("URI", uri.getPath());
    DataApi.DataItemResult result = Wearable.DataApi.getDataItem(mGoogleApiClient,uri).await();

    DataMapItem item = DataMapItem.fromDataItem(result.getDataItem());
    Asset cardImageAsset = item.getDataMap().getAsset(CardUtil.CARD_IMAGE);
    //Asset barcodeImageAsset = item.getDataMap().getAsset(CardUtil.BARCODE_IMAGE);
    String card_type = item.getDataMap().getString(CardUtil.CARD_TYPE);
    Bitmap cardImage = BitmapFactory.decodeStream(Wearable.DataApi.getFdForAsset(mGoogleApiClient, cardImageAsset).await().getInputStream());
    //  Bitmap barcodeImage = BitmapFactory.decodeStream(Wearable.DataApi.getFdForAsset(mGoogleApiClient,barcodeImageAsset).await().getInputStream());
    GiftCard card = new GiftCard();
    card.setCardImage(cardImage);
    card.setCardName(card_type);
    card.setCardID(id);

    return card;
}

public class LoadCards extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Boolean doInBackground(Void... arg0) {
       // Uri uri = getIntent().getData().buildUpon().encodedPath("/counter").build();
        Uri uri = getUriForDataItem("/counter");
        Toast.makeText(c,uri.toString(),Toast.LENGTH_LONG).show();
        DataApi.DataItemResult result = Wearable.DataApi.getDataItem(mGoogleApiClient,uri).await();

        DataMapItem item = DataMapItem.fromDataItem(result.getDataItem());
        int card_count = Integer.parseInt(item.getDataMap().getString(CardUtil.CARD_COUNT));
       // int card_count = Integer.parseInt(loadCardCounter());
        if(card_count <= 0){
            Toast.makeText(c,"No cards available to show!",Toast.LENGTH_LONG).show();
        } else {
            for (int i = 1; i <= card_count; i++) {
                GiftCard c = loadCardData(i);
                cards.add(c);
            }
        }
        return null;

    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);

        //update the card list
        mAdapter = new CardListAdapter(c,cards);
        mListView.setAdapter(mAdapter);
       // mListView.setClickListener();


    }
}

private Uri getUriForDataItem(String path) {

     String nodeId = getNodeId();

    return new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).authority(nodeId).path(path).build();
}


private String getNodeId() {

    NodeApi.GetConnectedNodesResult nodesResult = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
    List<Node> nodes = nodesResult.getNodes();
    if (nodes.size() > 0) {

        return nodes.get(0).getId();
    } else {
        Toast.makeText(c,"NO NODES AVAILABLE",Toast.LENGTH_LONG).show();
    }
    return null;
}}

0 个答案:

没有答案