GoogleApiClient节点为空 [英] GoogleApiClient Node is null

查看:74
本文介绍了GoogleApiClient节点为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Android Wear应用,并且我想使Wear设备在单击按钮时在手机上启动意图.

I'm writing an Android Wear app and I want to make the wear device start an intent on the phone when a button is clicked.

为此,我首先要像这样建立与GoogleApiClient的连接

To do this, I've first set up the connection to the GoogleApiClient like this

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

        googleApiClient.connect();

然后单击按钮,运行sendMessage函数.这是功能

Then on button click the function sendMessage is run. This is the function

private void sendMessage(){
        if(mNode != null && googleApiClient != null && googleApiClient.isConnected()){
            Wearable.MessageApi.sendMessage(
                    googleApiClient, mNode.getId(),HELLO_WORLD_WEAR_PATH, null).setResultCallback(
                    new ResultCallback<MessageApi.SendMessageResult>() {
                        @Override
                        public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                            if(!sendMessageResult.getStatus().isSuccess()){
                                Log.e("TAG", "Failed to send message with status code: " + sendMessageResult.getStatus().getStatusCode());
                            }
                            else{
                                Log.e("TAG", "Success");
                            }
                        }
                    }
            );
        }
        else {
            Log.e("TAG","No nodes" + mNode);

        }
    }

问题在于该函数进入sendMessage的else语句中,这意味着mNode为空. mNode是Node类型的变量.

The problem is that the function goes into the else statement of sendMessage which means mNode is null. mNode is a variable of type Node.

要获取节点,我需要此功能

To get the node, I have this function

private void resolveNode() {
        Wearable.NodeApi.getConnectedNodes(googleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
            @Override
            public void onResult(NodeApi.GetConnectedNodesResult nodes) {
                for (Node node : nodes.getNodes()) {
                    mNode = node;
                }
            }
        });
    }

我已将手机连接到Android Studio中的Android Wear虚拟设备.

I've connected my phone with the Android Wear Virtual Device in Android Studio.

我在做什么错了?

推荐答案

调用googleApiClient.connect()时,连接会花费很短的时间,并且此调用不会阻塞.因此,您需要等到连接成功并通过调用onConnected()才能进行进一步的调用.

When you call googleApiClient.connect(), the connection takes a short amount of time and this call does not block. So you need to wait until the connection succeeds with a call to onConnected() before you do any further calls.

接下来,当您调用resolveNode()时,不会立即设置mNode变量. setResultCallback将在不久后执行,因此在处理结果之前,您不应调用sendMessage().

Next, when you call resolveNode(), the mNode variable is not set instantly. The setResultCallback is executed a short time later, and so you should not call sendMessage() until the result is processed.

因此,您需要确保此处的所有物品都正确订购.

So you need to ensure proper ordering for everything here.

(编辑)

经过评论后,发现问题是由与Google Play服务的连接失败引起的.这是因为使用的模拟器是旧的API 20设备,该设备具有旧版本的Google Play服务.使用最新的API 22仿真器将解决此问题.

After going through the comments, it turns out the problem was caused by the connection to Google Play Services failing. This was because the emulator used was an old API 20 device, which has an old version of Google Play Services. Using the latest API 22 emulator will resolve the issue.

这篇关于GoogleApiClient节点为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆