7

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
Bellerofont
  • 1,081
  • 18
  • 17
  • 16
dingyucong
  • 75
  • 1
  • 5

1 Answers1

8

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();

pkanev
  • 1,486
  • 1
  • 12
  • 20
  • It also don't work.My mean is how to import the android.app.Activity in nativescript plugin? because the com.alipay.sdk.app.PayTask() function must use it.The jar package's url is: http://aopsdkdownload.cn-hangzhou.alipay-pub.aliyun-inc.com/demo/AlipaySDK_No_UTDID.zip?spm=a219a.7629140.0.0.r5xKQ3&file=AlipaySDK_No_UTDID.zip. I want to call PayTask(). – dingyucong Jan 16 '17 at 02:20
  • In that case you could extend your own Application/Activity classes (https://docs.nativescript.org/runtimes/android/advanced-topics/extend-application-activity) in NativeScript, and then retrieving it as detailed in the following answer: http://stackoverflow.com/a/13994622/6408287 – pkanev Jan 16 '17 at 07:37
  • com.alipay.sdk.app.PayTask(Activity activity) must set a activity in jar package.how to set the activity in nativescript? – dingyucong Jan 22 '17 at 09:32
  • I use currentContext the question fixed. – dingyucong Jan 22 '17 at 10:52
  • The dude wrote in his post that he already tried the first approach so why have it in the answer? – basickarl Jun 17 '20 at 18:47