I have some trouble when develop the nativescript.I don't know how to get the Activity in nativescript plugin?
In 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);
}
When I develop a plugin in nativescript.I write this code:
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);
}
};
It can use the com.alipay.sdk.app.PayTask(). But context is wrong.I have try
var context = application.android.context;
and
var context = application.android.foregroundActivity;
But when App runing also has exception:
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.<init>(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
You can use the core-modules API to retrieve the application context/activity as described in: https://docs.nativescript.org/cookbook/application#using-the-android-application-context
import app = require("application");
var context = app.android.context;
or
var utilsAd = require("utils/utils").ad;
var context = utilsAd.getApplicationContext();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With