pmi*_*hna 51 java java-native-interface android android-ndk
我需要使用NDK和JNI将一些函数实现到Android应用程序中.
以下是我编写的C代码,我的问题是:
#include <jni.h>
#include <stdio.h>
jobject
Java_com_example_ndktest_NDKTest_ImageRef(JNIEnv* env, jobject obj, jint width, jint height, jbyteArray myArray)
{
jint i;
jobject object;
jmethodID constructor;
jobject cls;
cls = (*env)->FindClass(env, "com/example/ndktest/NDKTest/Point");
//what should put as the second parameter? Is my try correct, according to what
//you can find in .java file? I used this documentation: http://download.oracle.com/javase/6/docs/technotes/guides/jni/spec/functions.html#wp16027
constructor = (*env)->GetMethodID(env, cls, "<init>", "void(V)");
//http://download.oracle.com/javase/6/docs/technotes/guides/jni/spec/functions.html#wp16660
//Again, is the last parameter ok?
object = (*env)->NewObject(env, cls, constructor, 5, 6);
//I want to assign "5" and "6" to point.x and point.y respectively.
return object;
}
Run Code Online (Sandbox Code Playgroud)
在代码中或多或少地解释了我的问题.也许还有:函数的返回类型(jobject)好吗?
现在NDKTest.java:
package com.example.ndktest;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
public class NDKTest extends Activity {
/** Called when the activity is first created. */
public native Point ImageRef(int width, int height, byte[] myArray);
public class Point
{
Point(int myx, int myy)
{
x = myx;
y = myy;
}
int x;
int y;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
byte[] anArray = new byte[3];
for (byte i = 0; i < 3; i++)
anArray[i] = i;
Point point = ImageRef(2, 3, anArray);
tv.setText(String.valueOf(point.x));
setContentView(tv);
}
static
{
System.loadLibrary("test");
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行代码时,它不起作用.
Hen*_*olm 81
既然Point是一个内部阶级,那么获得它的方式就是
jclass cls = (*env)->FindClass(env, "com/example/ndktest/NDKTest$Point");
Run Code Online (Sandbox Code Playgroud)
$权威规范中没有明确记录内部类的约定,但是在如此多的工作代码中根深蒂固,它不太可能改变.但是,如果您将JNI代码限制为使用顶级类,那么它会感觉更强大一些.
您需要一个以两个整数作为参数的构造函数.签名就是(II)V这样:
constructor = (*env)->GetMethodID(env, cls, "<init>", "(II)V");
Run Code Online (Sandbox Code Playgroud)
下一次,在代码中包含一些错误处理,这样你就可以知道它的哪一部分不起作用!
你的代码有些问题.
首先,为什么要创建自己的Point类而不是使用库提供的android.graphics.Point?
其次,嵌套类的类规范是不同的 - 它将是"com/example/ndktest/NDKTest $ Point".类嵌套与包不同.
第三,我不认为JNI允许您创建非静态嵌套类的实例.你需要this在创建对象时传递嵌套类对象' 指针 - 没有这样的参数.
最后,虽然我已经看到使用"void(V)"作为构造函数方法签名的指导,但这与其他方法签名不一致; 通常,具有两个int参数和void返回类型的方法将是"(II)V".
作为旁注,我发现将原始类型和从NDK类型化的原始数组传递给Java要简洁得多.对象创建/访问很麻烦,很难调试.