2016-11-17 91 views
1

我想延長GoogleApiClient.ConnectionCallbacks(),但要得到一個錯誤的ClassNotFoundException在NativeScript使用.extend()

`java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nativescript.sampleGroceries/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed

Error calling module function

Error: java.lang.ClassNotFoundException: com.google.android.gms.common.api.GoogleApiClient_ConnectionCallbacks java.lang.Class.classForName(Native Method) java.lang.Class.forName(Class.java:324) java.lang.Class.forName(Class.java:285) com.tns.DexFactory.generateDex(DexFactory.java:262) com.tns.DexFactory.resolveClass(DexFactory.java:120) com.tns.ClassResolver.resolveClass(ClassResolver.java:45)

`

我相應的代碼是

var GoogleApiClient = com.google.android.gms.common.api.GoogleApiClient; 
var myConnectionCallbacks = GoogleApiClient.ConnectionCallbacks.extend({ 
onConnected: function(connectionHint){ 
    var MessageListener = MessageListener.extend({ 
      onFound: function(message){ 
       Log.d("MESSAGE", "Found message :) "); 
      }, 
      onLost: function(message){ 
       Log.d("MESSAGE", "Lost message :("); 
      } 


    }); 
    var mMessageListener = new MessageListener(); 

}, 
onConnectionSuspended: function(){} 

}); 
    var MessagesOptionsBuilder = new com.google.android.gms.nearby.messages.MessagesOptions.Builder().setPermissions(NearbyPermissions.BLE).build(); 
    var test = new GoogleApiClient.Builder(context).addApi(Nearby.MESSAGES_API, MessagesOptionsBuilder); 
    test.addConnectionCallbacks(new myConnectionCallbacks(this)); 

也許我誤解了Docs? https://docs.nativescript.org/runtimes/android/generator/extend-class-interface

編輯:爲什麼在GoogleApiClient之後的錯誤日誌中有下劃線?

任何建議我做錯了什麼?

回答

2

這裏的問題是你試圖擴展一個錯誤的接口。更多信息 - >https://docs.nativescript.org/runtimes/android/generator/extend-class-interface#interfaces

var MessagesOptionsBuilder = new com.google.android.gms.nearby.messages.MessagesOptions.Builder().setPermissions(NearbyPermissions.BLE).build(); 
var test = new GoogleApiClient.Builder(context).addApi(Nearby.MESSAGES_API, MessagesOptionsBuilder); 
test.addConnectionCallbacks(new com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks({ 
    onConnected: function (connectionHint) { 
     var MessageListener = MessageListener.extend({ 
      onFound: function (message) { 
       console.log(Found message :) "); 
      }, 
      onLost: function (message) { 
       console.log("Lost message :("); 
      } 


     }); 
     var mMessageListener = new MessageListener(); 

    }, 
    onConnectionSuspended: function() { } 

})); 

和只是櫃面了MessageListener是一個接口的只是刪除.extend

var MessagesOptionsBuilder = new com.google.android.gms.nearby.messages.MessagesOptions.Builder().setPermissions(NearbyPermissions.BLE).build(); 
var test = new GoogleApiClient.Builder(context).addApi(Nearby.MESSAGES_API, MessagesOptionsBuilder); 
test.addConnectionCallbacks(new com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks({ 
    onConnected: function (connectionHint) { 
     new MessageListener({ 
      onFound: function (message) { 
       console.log(Found message :) "); 
      }, 
      onLost: function (message) { 
       console.log("Lost message :("); 
      } 


     }); 

    }, 
    onConnectionSuspended: function() { } 

})); 
+1

我應該學會如何閱讀,謝謝! :) –