简体   繁体   中英

JNI Java method gets jettisoned when called from c++

I have a project that works on C++, and now porting it to use java using JNI, all computations still are made mostly in C++ but it is now wrapped with Java, and i've stumbled on to problem, there is a Java object that i need to create, but its dependant on some computations and since i do those computations in C++ i decided just to create that object after those computations are done in C++

JNI works ok, because there were multiple calls before, that work just fine and breakpoints work (Java methods are called from native).

Java is called from C++

C++:

//after computations, C++ calls this method to call Java
extern "C"
{
    void CreateObject()
    {
        JNIEnv *jenv = GetJavaEnv(); 
        static jmethodID jmethod = GetJavaMethod(l_this, jenv, "CreateObject", "()V");
        jenv->CallVoidMethod(l_this,jmethod)
    }
}

Java

public class MainActivity
{
    private MyObject myObject = null;
    //lang and config are set from native by other calls, when i enter Create object, 
    //they look fine
    private String lang = null;

    public void CreateObject()
    {
        HashMap<AdConfigKey, String> config = new HashMap<AdConfigKey, String>();
        config.put("my string1", "string1");
        config.put("my string2", "string2");
        config.put("my string3", "string3");

        //at this point config looks fine, it's created and parameters are inside...
        MyObject = new MyObject(this, config);
        MyObject.setListener(this);
        MyObject.setLanguage(lang);
    }
}

the thing is that the call to the Java is executed, parameters are fine, but the allocation or constructor call never happens, that application don't throw any exceptions or errors, execution continues, but the object is never create, please sugest what might be the problem here...

It appears the third party software was making calls to UI elements, and that was the reason of jettison, still, i don't understand why there was no run time errors...

the solution code would be:

public void CreateObject()
{
    runOnUiThread(new Runnable() 
    {   
        public void run()
        {
            HashMap<AdConfigKey, String> config = new HashMap<AdConfigKey, String>();
            config.put("my string1", "string1");
            config.put("my string2", "string2");
            config.put("my string3", "string3");

            MyObject = new MyObject(this, config);
            MyObject.setListener(this);
            MyObject.setLanguage(lang);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM