我在开发nativescript时遇到了一些麻烦.我不知道如何在nativescript插件中获取Activity?
在android中:
import android.app.Activity; import com.alipay.sdk.app.PayTask; public Alipay(Activity activity) { this.activity = activity; PayTask alipay = new PayTask(activity); String result = alipay.pay(payInfo); }
当我在nativescript中开发插件时.我写下这段代码:
var application = require("application"); var context = application.android.context; //var context = application.android.foregroundActivity; module.exports = { startpay: function(orderInfo) { var payTask = new com.alipay.sdk.app.PayTask(context); return payTask.payV2(orderInfo,true); } };
它可以使用com.alipay.sdk.app.PayTask().但背景是错误的.我试过
var context = application.android.context;
和
var context = application.android.foregroundActivity;
但是,当App运行时也有异常:
An uncaught Exception occurred on "main" thread. com.tns.NativeScriptException: Calling js method onClick failed Error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference com.alipay.sdk.sys.b.a(SourceFile:46) com.alipay.sdk.app.PayTask.(SourceFile:57) com.tns.Runtime.callJSMethodNative(Native Method) com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1022) com.tns.Runtime.callJSMethodImpl(Runtime.java:907) com.tns.Runtime.callJSMethod(Runtime.java:895) com.tns.Runtime.callJSMethod(Runtime.java:879) com.tns.Runtime.callJSMethod(Runtime.java:871) com.tns.gen.android.view.View_OnClickListener.onClick(android.view.View$OnClickListener.java) android.view.View.performClick(View.java:4790) android.view.View$PerformClick.run(View.java:19933) android.os.Handler.handleCallback(Handler.java:739) android.os.Handler.dispatchMessage(Handler.java:95) android.os.Looper.loop(Looper.java:135) android.app.ActivityThread.main(ActivityThread.java:5569) java.lang.reflect.Method.invoke(Native Method) java.lang.reflect.Method.invoke(Method.java:372) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726) File: "/data/data/com.zhengbancn.app/files/app/tns_modules/nativescript-alipay/index.js, line: 8, column: 15
pkanev.. 7
您可以使用核心模块API来检索应用程序上下文/活动,如下所述:https: //docs.nativescript.org/cookbook/application#using-the-android-application-context
import app = require("application");
var context = app.android.context;
要么
var utilsAd = require("utils/utils").ad;
var context = utilsAd.getApplicationContext();
您可以使用核心模块API来检索应用程序上下文/活动,如下所述:https: //docs.nativescript.org/cookbook/application#using-the-android-application-context
import app = require("application");
var context = app.android.context;
要么
var utilsAd = require("utils/utils").ad;
var context = utilsAd.getApplicationContext();