Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Unable to call the c++ method from android native

Unable to call the c++ method from android native

Scheduled Pinned Locked Moved Mobile and Embedded
2 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kranthi
    wrote on last edited by
    #1

    Hi,
    I want to call c/c++ function from android java code, when i register the c methods using QAndroidJniEnvironment
    I am not able to call the java method from c++. if dont register, then i can able to call java methods from c++. But communication should happened both the ways. below it the sample code i have used for registering native methods.
    @
    void registerNativeMethods() {
    JNINativeMethod methods[] = {{"callNativeOne", "(I)V", reinterpret_cast<void *>(fromJavaOne)},
    {"callNativeTwo", "(I)V", reinterpret_cast<void *>(fromJavaTwo)}};

    QAndroidJniObject javaClass("org/qtproject/example/notification/NotificationClient");
    qDebug()<<"java class is valid? <<"<<javaClass.isValid();
    QAndroidJniEnvironment env;
    jclass objectClass = env->GetObjectClass(javaClass.object<jobject>());
    

    qDebug()<<"registernative returned ::"<< env->RegisterNatives(objectClass,
    methods,2
    /sizeof(methods) / sizeof(methods[0]/);
    env->DeleteLocalRef(objectClass);
    }
    @

    Please provide the solution for this problem

    Thanks and Regards
    Kranthi Talluri

    [edit: Added missing coding tags @ SGaist]

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      zsb123
      wrote on last edited by
      #2

      Hello!

      I had the same issue. I've spent my whole day trying to figure out what can the problem be. Now that I figured it out, I felt it important to register to this forum and share the solution with you.

      So... At the end of your registerNativeMethods() method add the following code:
      @ if (env->ExceptionCheck()) {
      // Handle exception here.
      env->ExceptionClear();
      }@

      You should also check "this part":http://qt-project.org/doc/qt-5/qandroidjniobject.html#handling-java-exception of the documentation.

      Let me know if it helped for you as well.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved