附近连接的Android应用程序问题

时间:2018-05-21 22:12:08

标签: android android-things google-nearby

我想在app编程中了解有关Nearby的更多信息。为此,我在GitHub https://github.com/PaulTR/NearbyConnections2.0Sample上下载了此代码。我的问题是,当我想发送消息时,移动应用程序崩溃了。好像是,Android Thing设备并没有真正连接到手机。

失败:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.tutsplus.nearbyconnections2, PID: 10951
                  java.lang.IllegalStateException: Could not execute method for android:onClick
                      at android.view.View$DeclaredOnClickListener.onClick(View.java:4732)
                      at android.view.View.performClick(View.java:5647)
                      at android.view.View$PerformClick.run(View.java:22479)
                      at android.os.Handler.handleCallback(Handler.java:761)
                      at android.os.Handler.dispatchMessage(Handler.java:98)
                      at android.os.Looper.loop(Looper.java:156)
                      at android.app.ActivityThread.main(ActivityThread.java:6623)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Method.invoke(Native Method)
                      at android.view.View$DeclaredOnClickListener.onClick(View.java:4727)
                      at android.view.View.performClick(View.java:5647) 
                      at android.view.View$PerformClick.run(View.java:22479) 
                      at android.os.Handler.handleCallback(Handler.java:761) 
                      at android.os.Handler.dispatchMessage(Handler.java:98) 
                      at android.os.Looper.loop(Looper.java:156) 
                      at android.app.ActivityThread.main(ActivityThread.java:6623) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832) 
                   Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
                      at com.google.android.gms.common.api.internal.zzbc.zze(Unknown Source)
                      at com.google.android.gms.common.api.internal.zzbl.zze(Unknown Source)
                      at com.google.android.gms.common.api.internal.zzbd.zze(Unknown Source)
                      at com.google.android.gms.internal.zzcnr.sendPayload(Unknown Source)
                      at com.tutsplus.nearbyconnections2.MainActivity.sendPayload(MainActivity.java:122)
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at android.view.View$DeclaredOnClickListener.onClick(View.java:4727) 
                      at android.view.View.performClick(View.java:5647) 
                      at android.view.View$PerformClick.run(View.java:22479) 
                      at android.os.Handler.handleCallback(Handler.java:761) 
                      at android.os.Handler.dispatchMessage(Handler.java:98) 
                      at android.os.Looper.loop(Looper.java:156) 
                      at android.app.ActivityThread.main(ActivityThread.java:6623) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832) 

有人知道问题是什么吗?

祝福Thorsten

1 个答案:

答案 0 :(得分:0)

private void setGoogleApiClient() {
    if (googleApiClient != null)
        return;
    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    googleApiClient.connect();
}

实施GoogleApiClient.ConnectionCallbacks和         GoogleApiClient.OnConnectionFailedListener您将能够检测到GoogleApiClient成功连接或由于某种原因连接失败。此外,在执行您的操作以避免可能的崩溃之前,您可以再次检查GoogleApiClient是否已连接:

if (!googleApiClient.isConnected())
            googleApiClient.connect();
else
// perform action