【发布时间】:2020-11-14 21:33:20
【问题描述】:
我正在按照下面的教程进行操作,但在 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();
}
}
然后我使用插件种子创建了一个 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
知道我错过了什么吗?由于我是 nativescript 的新手,请多多包涵。
谢谢, 瑞恩
【问题讨论】:
标签: android jar nativescript