使用JNI错误调用端点AsyncTask

时间:2014-04-16 16:58:20

标签: android c++ google-app-engine android-ndk google-cloud-endpoints

我正在使用Appengine作为后端开发Android的多人游戏。一切都很好,然后我为Android生成了端点代码,这也非常好用。

执行Endpoints类的Java类。

java代码:

public static void saveData()
{
    new EndpointsTask().execute(mActivity.getApplicationContext());
}

public class EndpointsTask extends AsyncTask<Context, Integer, Long> {
    protected Long doInBackground(Context... contexts) {
        Noteendpoint.Builder endpointBuilder = new Noteendpoint.Builder(
            AndroidHttp.newCompatibleTransport(),
            new JacksonFactory(),
            new HttpRequestInitializer() {
                public void initialize(HttpRequest httpRequest) { }
            });
        Noteendpoint endpoint = CloudEndpointUtils.updateBuilder(
            endpointBuilder).build();
        try {
            Note note = new Note().setDescription("Note Description");
            //String noteID = new Date().toString();
            String noteID = "lalala";
            note.setId(noteID);

            note.setEmailAddress("E-Mail Address");      
            Note result = endpoint.insertNote(note).execute();
        }
    }
}

然后从NDK NativeActivity c ++代码调用:

void CCHelper::saveData()
{
    PluginJniMethodInfo minfo;

    if(PluginJniHelper::getStaticMethodInfo(minfo, "com/util/Helper",  "saveData", "()V"))
    {
        minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID,NULL);
        minfo.env->DeleteLocalRef(minfo.classID);
    }
}

当我使用Jni从C ++调用它时,我收到错误:

  

04-16 17:31:20.178:W / dalvikvm(28843):dvmFindClassByName拒绝'com / util / Helper'   04-16 17:32:16.553:A / libc(28843):0x00000000处的致命信号11(SIGSEGV)(代码= 1)

我正在使用基于NativeActivity的应用程序(cocos2d-x)。

有解决方案吗?是否可以从NativeActivity调用端点代码?

1 个答案:

答案 0 :(得分:0)

所以我改变了我的C ++实现,使用std :: thread

调用JNI
std::thread t (&HelloWorld::ThreadFunction,this);
t.join();

void HelloWorld::ThreadFunction()
{
    CCHelper::saveData();
}

java代码,我删除了AsyncTask执行:

public static void saveData()
{
    Noteendpoint.Builder endpointBuilder = new Noteendpoint.Builder(
        AndroidHttp.newCompatibleTransport(),
        new JacksonFactory(),
        new HttpRequestInitializer() {
            public void initialize(HttpRequest httpRequest) { }
    });

    Noteendpoint endpoint = CloudEndpointUtils.updateBuilder(
        endpointBuilder).build();
    try {
        Note note = new Note().setDescription("Note Description");
        String noteID = new Date().toString();
        note.setId(noteID);

        note.setEmailAddress("E-Mail Address");      
        Note result = endpoint.insertNote(note).execute();
    } 
    catch (IOException e) {
        e.printStackTrace();
    }
}

内部PluginJniHelper :: getStaticMethodInfo JNI代码

...
if(!JAVAVM)
{
    LOGD("JavaVM is NULL");
    break;
}

if (JAVAVM->GetEnv((void**)env, JNI_VERSION_1_6) != JNI_OK)
{
    LOGD("Failed to get the environment using GetEnv()");
    break;
}

if (JAVAVM->AttachCurrentThread(env, 0) < 0)
{
    LOGD("Failed to get the environment using AttachCurrentThread()");
    break;
}
...

我收到错误无法使用GetEnv()获取环境,这是调用GetEnv()时VM失败。

我在这里找不到什么东西?