遵循以下教程操作,但是在nativescript中使用JAR文件时遇到问题。
https://nativescript.org/blog/plugins-and-jars/
我可以使用以下代码在Android Studio中生成一个JAR文件。
package com.testing.toaster; import android.content.Context; import android.widget.Toast; public class Toaster { public void show(Context context) { CharSequence text ="Hello NativeScript!"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); } }
然后,我使用插件 seed 创建了一个nativscript项目。创建后,该插件可以正常工作。
我在src / platforms / android / libs文件夹下添加了ToastPlugin.jar。
这是使用JAR文件的nativescript插件中的代码。
import { Observable } from 'tns-core-modules/data/observable'; import * as app from 'tns-core-modules/application'; import * as dialogs from 'tns-core-modules/ui/dialogs'; declare var com:any; export class Common extends Observable { public message: string; constructor() { super(); this.message = Utils.SUCCESS_MSG(); } public greet() { return "Hello, NS"; } } export class Utils { public static SUCCESS_MSG(): string { let msg = `Your plugin is working on ${app.android ? 'Android' : 'iOS'}.`; console.log("Ryan1:" + app.android.context); var toaster = new com.testing.toaster.Toaster(); //toaster.show(app.android.context); return msg; } }
这是我遇到的错误。
Restarting application on device emulator-5554... Successfully synced application org.nativescript.demo on device emulator-5554. JS: HMR: Hot Module Replacement Enabled. Waiting for signal. JS: Ryan1:com.tns.NativeScriptApplication@129d41d System.err: An uncaught Exception occurred on "main" thread. System.err: Unable to start activity ComponentInfo{org.nativescript.demo/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed System.err: Error: Building UI from XML. @app-root.xml:1:1 System.err: > Cannot read property 'toaster' of undefined System.err: System.err: StackTrace: System.err: java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nativescript.demo/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed System.err: Error: Building UI from XML. @app-root.xml:1:1 System.err: > Cannot read property 'toaster' of undefined System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913) System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) System.err: at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) System.err: at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) System.err: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) System.err: at android.os.Handler.dispatchMessage(Handler.java:106) System.err: at android.os.Looper.loop(Looper.java:193) System.err: at 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: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) System.err: Caused by: com.tns.NativeScriptException: Calling js method onCreate failed System.err: Error: Building UI from XML. @app-root.xml:1:1 System.err: > Cannot read property 'toaster' of undefined System.err: at com.tns.Runtime.callJSMethodNative(Native Method) System.err: at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1286) System.err: at com.tns.Runtime.callJSMethodImpl(Runtime.java:1173) System.err: at com.tns.Runtime.callJSMethod(Runtime.java:1160) System.err: at com.tns.Runtime.callJSMethod(Runtime.java:1138) System.err: at com.tns.Runtime.callJSMethod(Runtime.java:1134) System.err: at com.tns.NativeScriptActivity.onCreate(NativeScriptActivity.java:19) System.err: at android.app.Activity.performCreate(Activity.java:7136) System.err: at android.app.Activity.performCreate(Activity.java:7127) System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) System.err: ... 11 more
解决
您遇到的问题之一是,您所关注的教程是2015年的,此后平台已发生了重大变化。如果您想学习更多与本机层的交互,我的建议是在此处查看“构建插件”指南:https : //docs.nativescript.org/plugins/building-plugins