无法读取未定义的 属性 MyToast.java

Cannot read property MyToast.java of undefined

我一直在关注这个Tutorial来实现Write Java code in nativescript and use directly in typescript

但是我得到一个错误Cannot read property 'MyToast' of undefined

app.component.ts:

import {Component} from "@angular/core";
let application = require("application");

declare var org:any;
@Component({
    selector: "my-app",
    templateUrl: "app.component.html",
})
export class AppComponent {


    public onTap() {
        org.example.MyToast.showToast(application.android.context,"You pressed the button","short");
    }
}

我已经在 platforms-> src -> java -> org ->example 中创建 MyToast.java class :

package org.example;

import android.widget.Toast;
import android.content.Context;


public class MyToast{

    public static void showToast(Context context,String text ,String StrDuration ){

        Toast.makeText(context,text, Toast.LENGTH_SHORT).show();
    }
}

您执行构建了吗? JavaScript->Java 调用的元数据不会在未触发构建的简单 tns run android 中生成。

如果您触摸 platforms/android,将不会触发构建,因为该目录未被监视。

tns build android - 这将确保在 platforms/android 中进行手动更改时将触发构建。

Offtopic:另外,我很高兴与大家分享,在 NativeScript 4.0 版本中,您将能够直接在 App_Resources/Android 目录中创建这些 classes,而无需担心您的自定义 class 更换工作站时被擦干净,清理您的项目。