可以帮助我。我尝试在 Nativescript 中实现 Android 库。
我创建 Android 项目 -> 新模块 -> Andorid 库。
创建 Java 类 platforms\android\nativescript-serial-port\serialport\src\main\java\com\nativescript\serialport\Toaster.java
package com.nativescript.serialport;
import android.content.Context;
import android.widget.Toast;
public class Toaster {
public Toaster() {}
public void show(Context context) {
Toast.makeText(context, "Hello NativeScript!", Toast.LENGTH_SHORT).show();
}
}
添加文件 nativescript 项目platforms\android\settings.gradle
include ':app', ':serialport'
project(':serialport').projectDir = new File('nativescript-serial-port/serialport')
添加文件 nativescript app app\App_Resources\Android\app.gradle
dependencies {
implementation project(":serialport")
}
在 JS 文件中添加 nativescript 项目some- file.js
import * as app from 'application';
const context = android.content.Context;
const toaster = new com.nativescript.serialport.Toaster();
toaster.show(context);
如果我运行此代码出现错误
System.err:TypeError:com.nativescript.serialport.Toaster 不是构造函数 System.err:文件:“file:///data/data/org.nativescript.application/files/app/bundle.js,行:372 ,列:22 System.err: System.err: StackTrace: System.err: Frame: function:'onPrint', file:'file:///data/data/org.nativescript.application/files/app/bundle. js',行:372,列:23 System.err:框架:函数:'invoker',文件:'file:///data/data/org.nativescript.application/files/app/vendor.js',行:4168,列:18 System.err:框架:函数:'Observable.notify',文件:'file:///data/data/org.nativescript.application/files/app/vendor.js',行:17698 ,列:15 System.err:帧:函数:'Observable._emit',文件:'file:///data/data/org。nativescript.application/files/app/vendor.js',行:17726,列:12 System.err:帧:函数:'ClickListenerImpl.onClick',文件:'file:///data/data/org.nativescript。 application/files/app/vendor.js',行:29193,列:15 System.err:System.err:在 com.tns.Runtime.callJSMethodNative(本机方法) System.err:在 com.tns.Runtime.dispatchCallJSMethodNative (Runtime.java:1120) System.err: 在 com.tns.Runtime.callJSMethodImpl(Runtime.java:1000) System.err: 在 com.tns.Runtime.callJSMethod(Runtime.java:987) System.err: 在com.tns.Runtime.callJSMethod(Runtime.java:967) System.err: 在 com.tns.Runtime.callJSMethod(Runtime.java:959) System.err: 在 com.tns.gen.java.lang.Object_vendor_29183_26_ClickListenerImpl。onClick(Object_vendor_29183_26_ClickListenerImpl.java:17) System.err: 在 android.view.View.performClick(View.java:6597) System.err: 在 android.view.View.performClickInternal(View.java:6574) System.err:在 android.view.View.access$3100(View.java:778) System.err: 在 android.view.View$PerformClick.run(View.java:25885) System.err: 在 android.os.Handler.h andleCallback (Handler.java:873) System.err: 在 android.os.Handler.dispatchMessage(Handler.java:99) System.err: 在 android.os.Looper.loop(Looper.java:193) System.err: 在android.app.ActivityThread.main(ActivityThread.java:6669) System.err: at java.lang.reflect.Method.invoke(Native Method) System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493) System.err: 在 com.android。internal.os.ZygoteInit.main(ZygoteInit.java:858)
如何修复此错误。谢谢