使用.extend()在NativeScript中的ClassNotFoundException

时间:2016-11-17 15:48:33

标签: javascript java android nativescript

我正在尝试扩展GoogleApiClient.ConnectionCallbacks(),但一直收到错误

  

`java.lang.RuntimeException:无法启动活动   ComponentInfo {org.nativescript.sampleGroceries / com.tns.NativeScriptActivity}:   com.tns.NativeScriptException:调用js方法onCreate失败

     

调用模块功能时出错

     

错误:java.lang.ClassNotFoundException:   com.google.android.gms.common.api.GoogleApiClient_ConnectionCallbacks       java.lang.Class.classForName(本机方法)       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之后错误日志中有下划线?

有什么建议我做错了吗?

1 个答案:

答案 0 :(得分: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 () { }

}));