Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I'm able to instantiate the class by the following statements:</p> </blockquote> <pre class="lang-cs prettyprint-override"><code>Java.Lang.Object jclassWrp_; IntPtr JavaScriptInterface_Class = JNIEnv.FindClass("mypackage.JavaScriptInterface"); </code></pre> <p>JNI use should use JNI conventions, thus <code>mypackage/JavaScriptInterface</code> (note <code>/</code> instead of <code>.</code>).</p> <pre><code>IntPtr JavaScriptInterface_ctor = JNIEnv.GetMethodID(JavaScriptInterface_Class, "&lt;init&gt;", "()V"); IntPtr jsInterfaceinstance_ = JNIEnv.NewObject(JavaScriptInterface_Class, JavaScriptInterface_ctor); jclassWrp_ = new Java.Lang.Object(jsInterfaceinstance_, JniHandleOwnership.TransferGlobalRef); </code></pre> <p><code>JNIEnv.NewObject()</code> returns a local ref, not a global ref, so you want <code>JniHandleOwnership.TransferLocalRef</code>.</p> <blockquote> <p>But when i try to create the object to access the getSelctd() method:</p> </blockquote> <pre class="lang-cs prettyprint-override"><code>IntPtr ipApid = JNIEnv.GetMethodID(jclassWrp_, "getSelctd", "()Ljava/lang/String;"); </code></pre> <p><code>JNIEnv.GetMethodID()</code> takes a class handle, not an instance. Firstly, the above shouldn't compile (<code>Java.Lang.Object</code> != <code>IntPtr</code>). Secondly, <code>jclassWrp</code> contains a <code>mypackage.JavaScriptInterface</code> instance, not the <code>mypackage.JavaScriptInterface</code> Class instance.</p> <p>Instead, do:</p> <pre class="lang-cs prettyprint-override"><code>IntPtr ipApid = JNIEnv.GetMethodID(JavaScriptInterface_Class, "getSelctd", "()Ljava/lang/String;"); </code></pre> <p>Finally, don't forget to <code>JNIEnv.DeleteGlobalRef(JavaScriptInterface_Class)</code> when you don't need it anymore, otherwise you'll leak the gref.</p> <p>Complete code:</p> <pre class="lang-cs prettyprint-override"><code>// FindClass() returns a gref; must be freed (see below) IntPtr JavaScriptInterface_Class = JNIEnv.FindClass("mypackage/JavaScriptInterface"); // MethodIDs do not need to be freed IntPtr JavaScriptInterface_ctor = JNIEnv.GetMethodID(JavaScriptInterface_Class, "&lt;init&gt;", "()V"); IntPtr JavaScriptInterface_getSelctd = JNIEnv.GetMethodID(JavaScriptInterface_Class, "getSelctd", "()Ljava/lang/String;"); // JNIEnv.NewObject() &amp; JNIEnv.CallObjectMethod() return lrefs; freed below IntPtr lrefInstance = JNIEnv.NewObject(JavaScriptInterface_Class, JavaScriptInterface_ctor); IntPtr lrefSelectd = JNIEnv.CallObjectMethod(jsInterfaceinstance_, ipApid); // JniHandleOwnership.TransferLocalRef causes lrefSelectd to be released for us string selected = JNIEnv.GetString(lrefSelectd, JniHandleOwnership.TransferLocalRef); // Resource cleanup JNIEnv.DeleteLocalRef(lrefInstance); JNIEnv.DeleteGlobalRef(JavaScriptInterface_Class); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload