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. Register native Function in Java Activity leads to crash
Forum Updated to NodeBB v4.3 + New Features

Register native Function in Java Activity leads to crash

Scheduled Pinned Locked Moved Solved Mobile and Embedded
1 Posts 1 Posters 852 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.
  • M Offline
    M Offline
    Matzurai
    wrote on last edited by Matzurai
    #1

    I tried to register a c++ function to the NotificationClient example (Qt 5.9).
    Following code was added to the NotificationClient constructor to register the c++ function.

    QAndroidJniEnvironment env;
    QAndroidJniObject::callStaticMethod<void>("android/os/Looper","prepare","()V");
    JNINativeMethod methods[] {{"test", "()V", reinterpret_cast<void*>(test)}};
    QAndroidJniObject javaClass("org/qtproject/example/notification/NotificationClient");
    jclass objectClass = env->GetObjectClass(javaClass.object<jobject>());
    env->RegisterNatives(objectClass, methods, sizeof(methods)/sizeof(methods[0]));
    env->DeleteLocalRef(objectClass);
    

    This lead to uncought exceptions saying, I can't register any handlers in a Thread before calling Looper.prepare(). After several failed attempts I found out, that registering a c++ function inside a Java Activity lead to this exception.
    So the next Idea was to call android/os/Looper prepare from the c++ part where I try to register the function. Indeed that prevented this exception, leading to the next one when trying to create a notification by tapping on the smileys:

    java.lang.IllegalStateException: System service not available to Activities before onCreate()

    You may note, that the Documentation about Looper.prepare advises you to call loop() (and quit() when finished), but doing this leads to a blanc ui. Interestingly, just calling looper.prepare without registering the native function does NOT lead to any exception.

    So I'm very confused right now. Any suggestions to fixing this?

    (btw. I'm actually trying to access the android stepsensor and wanted to add a callback in an activity implementing a sensorEventHandler, but I could narrow it down to the situation described above, but feel free to give me a working implementation for a QStepSensor =D )

    Edit: solved it by using this tutorial, solution was to register the function in the JNIEXPORT JNI_OnLoad in the main.cpp

    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