开发者

FindClass cannot find custom Java class

I have a purely native app, as I use the android_native_app_glue and the entrypoint to my app is

 android_main(..)

I also have a Java helper type class:

 package abc.def.ghi
 public class MyJavaClass extends Activity
 {
      public void callback() 
      {
          Log.v(TAG, "In MyJavaClass");
      }
 }

I want to call the 'callback' method from native. I do so by:

jclass myClass = env->FindClass("abc/def/ghi/MyJavaClass");
if( myClass == NULL )
    LOGI("myClass null");
jmethodID mid = m_pEnv->GetMethodID(myClass, "callback", "()V");
if( mid == NULL )
    LOGI("MID null");
m_pEnv->CallVoidMethod(myClass, mid);

However this results in a message that myClass is null. My 'env' pointer should be okay because when I pass "android/os/build" in FindClass, it finds it, but cannot find my custom class.

In the LOGCAT, I see: "JNI Warning: Can't call Labc/def/ghi; callback on instance of Ljava/Lang/Class;"

I th开发者_运维技巧ink this means something is wrong with the first parameter of my CallVoidMethod. I noticed in the other examples that most people perform their CallVoidMethod within a function with a signature that is generated by javah, and they use the obj parameter in their CallVoidMethod:

  JNIEXPORT void JNICALL function(JNIEnv *env, jobject obj, jstring s)

However I cannot use this because my app's entry point is in native.


However this results in a message that myClass is null. My 'env' pointer should be okay because when I pass "android/os/build" in FindClass, it finds it, but cannot find my custom class.

This would suggest your custom classes are not on the classpath.

Note that when you create a VM you can pass options, including setting the classpath to include the current directory ( or wherever you like ).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜