Class doesn' t implement Cloneable, implementcloneable
1. Error:
2. Field description:
During android project development, I want to use java code to call the test function of jni, and then call back the onCallback function of java through the callCallBack function of c. The above error occurs.
3. Code: (only important and key code is listed here .)
Java code:
Public classNativeClass{
Private static String TAG ="NativeClass";
Static {
System. loadLibrary ("Test-jni");
}
Public static intOnCallBack(Int event, int type, String str) {// callback function
Log. I (TAG, "onCallBack ======" + event );
Log. I (TAG, "onCallBack ======" + type );
Log. I (TAG, "onCallBack ======" + str );
Return event;
}
Public static native int test ();// Native Function
C code:
JobjectMy_obj;
JNIEnv *My_env;
Extern "C" int callback (int event, int type, const char * str );
Extern "C"
{
// Call java callback
IntCallCallBack(Int event, int type, const char * str)
{
Int err = 1000;
Jclass objClass = (my_env)->FindClass("Com/jnitest/native/NativeClass ");
If (! ObjClass ){
Return-1;
}
// Obtain and call the onCallBack function of the java Layer
JmethodID methodId = (my_env)->GetStaticMethodID(ObjClass ,"OnCallBack"," (IILjava/lang/String;) I ");
If (methodId = 0 ){
LOGDV ("here can not find method % s \ n", "onCallBack ");
} Else {
Jstring data = (update_env)-> NewStringUTF (str ));
Err = (my_env)->CallStaticIntMethod(ObjClass, methodId, event, type, data );
If (data) my_env-> DeleteLocalRef (data );
}
My_env-> DeleteLocalRef (objClass );
Return err;
}
// Jni native function.
JNIEXPORT jstring JNICALL
Java_com_jnitest_native _Test(JNIEnv * env, jobject thiz, jstring testStr)
{
My_env = env; // It must be saved and used to obtain the class of the callback function during callback.
My_obj = thiz; // This variable is not used in this program.
Const char * test_char = (env)-> GetStringUTFChars (testStr, NULL );
// Call callback
Int I =CallCallBack(0, 1, test_char );
Return (env)-> NewStringUTF (test_char );
}
}
Test code:
Maintest. java:
Void testFunc (){
String s = NativeClass. test ("ssss ");
Log. I ("jniTest", "testFunc s =" + s );
}
Test Results:
OnCallBack ======= 0
OnCallBack ======= 1
OnCallBack ====== ssss
TestFunc s = ssss
4. Analysis and Solution:
Analysis ,:
According to the error message:
1) There are clone errors;
2) It occurs in the test function of NativeClass.
3). Which class is not cloned? Or is it not the reason for clone?
4). NativeClass does not contain data members. Theoretically, cloning is not required. Therefore, it should not be the reason for NativeClass cloning.
5). In the test function of jni, is there a value assignment to my_obj? Will it be caused by the value assignment of this variable?
Solution: Because my_obj is not used (intended after being assigned a value) and may cause errors, remove the Declaration and assignment statement of this variable,
Test again and the program runs normally.
5. Conclusion:
1) when calling c in java, be sure to pass the correct parameters.
2) This example shows how to use java to call c and then use c to call java.
3) This example is from android program development.