需求:
在c回调函数里面,创建一个java对象,并将值传递给java应用使用。
void callback()
{
JNIEnv *env=NULL;
jobject searchResultObj;
pVm->AttachCurrentThread(&env,NULL);
jclass jclsProcess = env->GetObjectClass(cb.cbobj);
jmethodID jmidProcess3= env->GetMethodID(jclsProcess,"UserProcess","(ILandroid/aaa/Result;)I");
jclass ResultCls= env->FindClass("android/aaa/Result");
if(searchResultCls!=0)
{
HS_UTIL_Printf(LEVEL_ERROR, "findclass result ok");
jmethodID ResultId = env->GetMethodID(ResultCls,"<init>","(II)V");
ResultObj=env->NewObject(ResultCls,ResultId,1,2);
env->CallIntMethod(g_search_cb.m_objInterface, jmidProcess3, event, ResultObj);
}else
{
HS_UTIL_Printf(LEVEL_ERROR, "findclass result failed");
}
}
public class Result
{
public int a;
public int b;
public Result()
{
}
public SearchResult(int a,int b)
{
this.a=a;
this.b=b;
}
}
public interface Callback {
public abstract int UserProcess(int encryptType,Result info);
}
运行出现以下错误,
Class lookup LLandroid/aaa/Result;; attempted while exception Ljava/lang/NullPointerException; pending
解决了。
c线程和java线程之间还是有差别的。c线程只能操作java对象,不能创建java对象,尤其通过findclass这个方式。