How to reference 'this' inside an interface listener implemented with NativeScript?

走远了吗. 提交于 2019-12-08 07:23:18

问题


In Java, I have the documentation for the following code:

import com.creator.creatorandroidsdk.terminal.DeviceSelectionListener
import com.creator.creatorandroidsdk.TerminalListManager;

public class ConfigPinPadActivity extends Activity implements DeviceSelectionListener {

        TerminalListManager terminalListManager;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            terminalListManager = new TerminalListManager(this,getApplicationContext());
            terminalListManager.startTerminalsDiscovery();
        }

//...

}

Where this is sent to a class method as a reference to the instance of the implementation itself:

terminalListManager = new TerminalListManager(this,getApplicationContext());

I've tryied to replicate the code in NativeScript, but the reference doesn't work:

const { DeviceSelectionListener } = com.creator.creatorandroidsdk.terminal
const { TerminalListManager } = com.creator.creatorandroidsdk

const superProto = androidx.appcompat.app.AppCompatActivity.prototype

androidx.appcompat.app.AppCompatActivity.extend(
  'com.company.configPinPadActivity',
  {
    interfaces: [DeviceSelectionListener],
    terminalListManager: {},
    onCreate: function (savedInstanceState) {

      console.log('OnCreated fired') // <- It works.
      this.terminalListManager = new TerminalListManager(this, utilsModule.ad.getApplicationContext())

This particular error comes up.

JNI ERROR (app bug): attempt to pass an instance of com.company.configPinPadActivity as argument 1 to void com.creator.creatorandroidsdk.TerminalListManager.<init>(com.creator.creatorandroidsdk.terminal.DeviceSelectionListener, android.content.Context)

JNI DETECTED ERROR IN APPLICATION: bad arguments passed to void com.creator.creatorandroidsdk.TerminalListManager.<init>(com.creator.creatorandroidsdk.terminal.DeviceSelectionListener, android.content.Context) (see above for details)

I'm really trying to pass an instance of com.company.configPinPadActivity implementation to the new TerminalListManager, and since the com.company.configPinPadActivity implements a DeviceSelectionListener, I cannot understand why this isn't working.

How can reference the instance of the implementation from within the implementation itself?


回答1:


Looks like you are confusing TerminalListManager to being an interface. You cannot instantiate interfaces (new TerminalListManager), only implementations of such interfaces.

Would help if you add the source of TerminalListManager. If you are trying to pass 'this' to the TerminalListManager constructor, you need to have the the type of first parameter ConfigPinPadActivity (or Activity if you plan to re-use the TerminalListManager) or of type DeviceSelectionListener.



来源:https://stackoverflow.com/questions/57929536/how-to-reference-this-inside-an-interface-listener-implemented-with-nativescri

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!