【问题标题】:How do we use Android package JAR in NativeScript我们如何在 NativeScript 中使用 Android 包 JAR
【发布时间】: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 的新手,请多多包涵。

project in nativescript

谢谢, 瑞恩

【问题讨论】:

    标签: android jar nativescript


    【解决方案1】:

    您会遇到的一个问题是,您所遵循的教程是 2015 年的,从那时起平台发生了重大变化。

    通常,您包含本机代码的方式是通过插件。因此,了解您要实现的目标会很有用。如果您想显示 toast,请查看用于显示 toast 的现有插件(nativescript-toast 和 nativescript-toasty 将让您了解如何实现这一点)。

    如果您想学习与原生层进行更多交互,我的建议是查看此处的构建插件指南:https://docs.nativescript.org/plugins/building-plugins

    如果您浏览该教程,您会发现该过程更加直接,并且应该是最新的。还有一些关于如何与其他开发人员讨论插件开发的有用信息,您可能会觉得有帮助。

    【讨论】:

      猜你喜欢
      • 2011-01-19
      • 1970-01-01
      • 2012-06-23
      • 2016-09-05
      • 1970-01-01
      • 2021-08-25
      • 2020-05-13
      • 2011-10-17
      • 1970-01-01
      相关资源
      最近更新 更多