Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8448877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:32:34+00:00 2026-06-10T10:32:34+00:00

This is the Java class i’m accessing through JNIEnv in my monodroid application package

  • 0

This is the Java class i’m accessing through JNIEnv in my monodroid application

package mypackage;

import android.util.Log;

public class JavaScriptInterface {


    public String submitAns = "";

    // The JNI in the original question uses a default constructor.
    // Either provide one explicitly or use the implicit one...
    public JavaScriptInterface() 
    {

    }   

    public String getSelctd()
    {
        return submitAns;
    }
}

I’m able to instantiate the class by the following statements:

Java.Lang.Object jclassWrp_;

IntPtr JavaScriptInterface_Class = JNIEnv.FindClass("mypackage.JavaScriptInterface");

IntPtr JavaScriptInterface_ctor = JNIEnv.GetMethodID(JavaScriptInterface_Class, "<init>", "()V"); //(Landroid/context/Context;)V

IntPtr jsInterfaceinstance_ = JNIEnv.NewObject(JavaScriptInterface_Class, JavaScriptInterface_ctor);

jclassWrp_ = new Java.Lang.Object(jsInterfaceinstance_, JniHandleOwnership.TransferGlobalRef);

But when i try to create the object to access the getSelctd() method:

IntPtr ipApid = JNIEnv.GetMethodID(jclassWrp_, "getSelctd", "()Ljava/lang/String;");

It throws NoSuchMethodExist Exception…
Please tell me whether i’m doing it the right way and what i’m missing here…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:32:36+00:00Added an answer on June 10, 2026 at 10:32 am

    I’m able to instantiate the class by the following statements:

    Java.Lang.Object jclassWrp_;
    IntPtr JavaScriptInterface_Class = JNIEnv.FindClass("mypackage.JavaScriptInterface");
    

    JNI use should use JNI conventions, thus mypackage/JavaScriptInterface (note / instead of .).

    IntPtr JavaScriptInterface_ctor = JNIEnv.GetMethodID(JavaScriptInterface_Class, "<init>", "()V");
    IntPtr jsInterfaceinstance_ = JNIEnv.NewObject(JavaScriptInterface_Class, JavaScriptInterface_ctor);
    jclassWrp_ = new Java.Lang.Object(jsInterfaceinstance_, JniHandleOwnership.TransferGlobalRef);
    

    JNIEnv.NewObject() returns a local ref, not a global ref, so you want JniHandleOwnership.TransferLocalRef.

    But when i try to create the object to access the getSelctd() method:

    IntPtr ipApid = JNIEnv.GetMethodID(jclassWrp_, "getSelctd", "()Ljava/lang/String;");
    

    JNIEnv.GetMethodID() takes a class handle, not an instance. Firstly, the above shouldn’t compile (Java.Lang.Object != IntPtr). Secondly, jclassWrp contains a mypackage.JavaScriptInterface instance, not the mypackage.JavaScriptInterface Class instance.

    Instead, do:

    IntPtr ipApid = JNIEnv.GetMethodID(JavaScriptInterface_Class, "getSelctd", "()Ljava/lang/String;");
    

    Finally, don’t forget to JNIEnv.DeleteGlobalRef(JavaScriptInterface_Class) when you don’t need it anymore, otherwise you’ll leak the gref.

    Complete 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,
            "<init>", "()V");
    IntPtr JavaScriptInterface_getSelctd = JNIEnv.GetMethodID(JavaScriptInterface_Class,
            "getSelctd", "()Ljava/lang/String;");
    
    // JNIEnv.NewObject() & 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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which resides in ROOT. This application has a java class(in
When I try to compile this: import java.awt.* ; class obj { public static
For this example Java class: package foo; public class TestInterop { public String test(int
I'm passing values via PHP for this android java class(Login screen)but it's giving me
This is my gridview.java class public class TextGridActivity extends Activity { String[] _cataId=new String[100]
Considering this part of a Java class, private List<Object> components = new ArrayList<Object>(); private
in my java class we were learning about arrays and this question came up.
This is homework. Beginning Java class. Still wrapping my head around this stuff. The
I need this for calling a C function from Java class (JNI) and I
I have a file in .gz format. The java class for reading this file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.