JNI CALL change jclass parameter or how to obtain a jobject from a jclass parameter
I'm testing some functions with Android, JNI and NDK.
I have the following JAVA class:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class JNITest extends Activity {
private int contador;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 开发者_Python百科{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
contador = 0;
TextView label = (TextView)findViewById(R.id.Text);
label.setText(Integer.toString(contador));
}
public void addClick(View addButton) {
nativeAdd(1);
TextView label = (TextView)findViewById(R.id.Text);
label.setText(Integer.toString(contador));
}
private static native void nativeAdd(int value);
static {
System.loadLibrary("JNITest01");
}
}
I have used javah -jni
to generate header file:
#include <jni.h>
/* Header for class es_xxxxx_pruebas_JNITest */
#ifndef _JNITestNative
#define _JNITestNative
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_xxxxx_tests_JNITest
* Method: nativeAdd
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_com_xxxxx_tests_JNITest_nativeAdd
(JNIEnv *, jclass, jint);
#ifdef __cplusplus
}
#endif
#endif
As you can see, second paramter is jclass type.
I'm wondering how I can change jclass for a jobject parameter.
I need a jobject paramter to obtain a value from a field of the class is calling this native function.
How can I change method signature? or how can I obtain jobject from jclass paremeter?
Thanks.
Static methods do not have access to an Object (the implicit this
parameter), only access to other static methods/properties of that Class. That is why your native method has a jclass instead of a jobject.
So change your Java method to be nonstatic and regenerate your header file.
As an aside you can create Java objects from JNI but in this case I think you want to be able to change the value of the member variable contador
so that wouldnt help you.
精彩评论