NativeScript 中的 ClassNotFoundException 使用 .extend()
ClassNotFoundException in NativeScript using .extend()
我正在尝试扩展 GoogleApiClient.ConnectionCallbacks(),但一直出现错误
`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));
也许我误解了文档?
https://docs.nativescript.org/runtimes/android/generator/extend-class-interface
编辑:为什么在 GoogleApiClient 之后的错误日志中有下划线?
对我做错了什么有什么建议吗?
这里的问题是您正在尝试扩展一个错误的接口。更多信息 -> 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 () { }
}));
我正在尝试扩展 GoogleApiClient.ConnectionCallbacks(),但一直出现错误
`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));
也许我误解了文档? https://docs.nativescript.org/runtimes/android/generator/extend-class-interface
编辑:为什么在 GoogleApiClient 之后的错误日志中有下划线?
对我做错了什么有什么建议吗?
这里的问题是您正在尝试扩展一个错误的接口。更多信息 -> 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 () { }
}));