问题
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