从本地code错误调用Java函数 [英] Error calling java function from native code

查看:203
本文介绍了从本地code错误调用Java函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打电话从本地code的Java方法。我遵循的步骤: 调用从C ++中的Andr​​oid 的Java方法

I want to call a java method from native code. I followed the steps given in: Calling a java method from c++ in Android

不过我的应用程序崩溃,在那里的Java函数被调用的地方。

However my application crashes at the place where the java function is called.

我的Java code为如下图所示:

My java code is as shown below:

package com.example.jnitry;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d("MYAPP","Ocreate entered....");
        Button btn=(Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                mainfunc(8);
            }
        });
    }

    public void display(byte[] byt){
        Log.d("MYAPP", "display() is entered....");
        for(int i=0;i<byt.length;i++)
            Log.d("MYAPP", "byt["+i+"]="+byt[i]);
        Log.d("MYAPP", "display finished");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    public static native void mainfunc(int n);

    static
    {
        System.loadLibrary("mylib");
    }
}

我的母语code为如下图所示:

My native code is as shown below:

#include <jni.h>
#include "com_example_jnitry_MainActivity.h"
#include <android/log.h>
#include <stdio.h>

JNIEXPORT void JNICALL Java_com_example_jnitry_MainActivity_mainfunc
  (JNIEnv *env, jclass obj, jint n){

    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","mainfunction entered...",NULL);
    int i;
    unsigned char arr[10];
    jbyteArray bArray=(*env)->NewByteArray(env,n);

    jclass cls = (*env)->FindClass(env, "com/example/jnitry/MainActivity");
        jmethodID mid = (*env)->GetMethodID(env, cls, "display", "([B)V");
        if (mid == 0){
            __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","mid==0",NULL);
            return;
        }
        if(bArray==NULL)
            {   __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","bArray==NULL...",NULL);
                return ;
            }
    for(i=0;i<n;i++){
        arr[i]=i;
        __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","Iteration number:%d",i);
    }

    (*env)->SetByteArrayRegion(env,bArray,0,n,arr);
    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","bArray successfully created...",NULL);
    (*env)->CallVoidMethod(env, obj, mid, bArray);
    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","Returned from disp() java function.....",NULL);

}

打印日志消息后,我的应用程序崩溃:

My app crashes after printing the log message:

02-14 15:37:06.814: D/MYAPP(11584): bArray successfully created...

谁能告诉我的原因。并提供相同的溶液。先谢谢了。

Can anyone please tell me the reason. And provide a solution for the same. Thanks in advance.

注意:我已经尝试过的 CallVoidMedthodA() callByteMethod() CallObjectMethod(),但他们结果是一样的。

Note: I have already tried CallVoidMedthodA(), callByteMethod(), CallObjectMethod() but they outcome was the same.

推荐答案

您的本地方法 mainFunc 是一个静态方法 - 它没有一个有效的这个指针。同时,显示是一个实例方法 - 它需要一个

Your native method mainFunc is a static method - it does not have a valid this pointer. Meanwhile, display is an instance method - it needs one.

mainFunc 的第二个参数实际上是 MainActivity A类对象的指针,按照JNI的规则。声明 mainFunc 作为非静态的Java端,这将正常工作。

The second parameter of mainFunc is actually a class object pointer for MainActivity, as per JNI rules. Declare mainFunc as nonstatic on Java side, and this will work.

这篇关于从本地code错误调用Java函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆