简体   繁体   English

从C ++调用时会抛弃JNI Java方法

[英]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++ 我有一个可以在C ++上运行的项目,现在将它移植到使用JNI的Java中,所有计算仍主要在C ++中进行,但是现在它被Java封装了,我偶然发现了问题,有一个Java对象我需要创建,但是它依赖于某些计算,并且由于我在C ++中进行了这些计算,所以我决定仅在C ++中完成这些计算后才创建该对象

JNI works ok, because there were multiple calls before, that work just fine and breakpoints work (Java methods are called from native). JNI可以正常工作,因为之前有多次调用,所以可以正常工作,并且断点也可以工作(从本地调用Java方法)。

Java is called from C++ 从C ++调用Java

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... 事实是对Java的调用已执行,参数很好,但是分配或构造函数调用从未发生,该应用程序不引发任何异常或错误,执行继续,但该对象从未创建,请提出可能的建议成为这里的问题...

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... 看来第三方软件正在调用UI元素,这就是抛弃的原因,但我仍然不明白为什么没有运行时错误...

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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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