Re: Can't find method in Java class

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: Can't find method in Java class

Jeff Gosling
I had the same problem - thanks for this, it really helped.

Seems so obvious once you know the reason :)

On Tuesday, 25 May 2010 14:26:37 UTC+1, Angus Lees wrote:
The clue is "Ljava/lang/Class" in your error message:
> I/dalvikvm(  809): Method not found: 'SwapBuffers' '()I' in Ljava/lang/Class;

You've made NativeInitJavaCallbacks a static.  So when it calls JNI,
your thiz object is actually a class object, not an instance of
YGRenderer.  You then find the class of the class object, and then try
to look up SwapBuffers() method on java.lang.Class ;)

Remove "static" from the NativeInitJavaCallbacks Java declaration.

 - Gus

On Tue, May 25, 2010 at 16:51, knight666 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="17NcKJ4kbEkJ">knig...@...> wrote:

> Hi,
>
> so I finally decided to use the NDK like it's supposed to. I've made a
> very simple Java class and am trying to call a method from native
> code, to swap the OpenGL ES buffer.
>
> This is my class:
>
> class YGRenderer implements GLSurfaceView.Renderer
> {
>
>        public void onSurfaceCreated(GL10 gl, EGLConfig config)
>        {
>                /*mEGLConfigChooser = new SimpleEGLConfigChooser(true);
>                mEGLContextFactory = new DefaultContextFactory();
>                mEGLWindowSurfaceFactory = new DefaultWindowSurfaceFactory();*/
>
>                m_EGL = (EGL10)EGLContext.getEGL();
>
>                m_EGLDisplay = m_EGL.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
>
>                m_EGLContext = m_EGL.eglCreateContext(m_EGLDisplay, config,
> EGL10.EGL_NO_CONTEXT, null);
>
>                int[] version = new int[2];
>                m_EGL.eglInitialize(m_EGLDisplay, version);
>
>                //m_EGLConfig = m_EGLConfigChooser.chooseConfig(m_EGL,
> m_EGLDisplay);
>                // eglChooseConfig(m_Display, EGL_R5G6B5, &m_Config, 1,
> &confim_total)
>                int[] num_config = new int[1];
>                //egl.eglChooseConfig(m_EGLDisplay, EGL10.EGL_R5G6B5, null, 0,
> num_config);
>
>
>                /*m_EGLContext = m_EGLContextFactory.createContext(m_EGL,
> m_EGLDisplay, m_EGLConfig);
>                if (m_EGLContext == null || m_EGLContext == EGL10.EGL_NO_CONTEXT)
>                {
>                throw new RuntimeException("createContext failed");
>                }*/
>
>                //m_EGLSurface = null;
>                //egl.eglCreateWindowSurface(display, config, nativeWindow, null)
>
>                NativeInitJavaCallbacks();
>                NativeInit();
>        }
>
>        public void onSurfaceChanged(GL10 gl, int w, int h)
>        {
>                NativeResize(w, h);
>        }
>
>        public void onDrawFrame(GL10 gl)
>        {
>                NativeRender();
>        }
>
>        public int SwapBuffers()
>        {
>                //return super.SwapBuffers() ? 1 : 0;
>                //mEgl.eglSwapBuffers(mEglDisplay, mEglSurface);
>                return 0;
>        }
>
>        /*
>                Native functions
>        */
>
>        private static native void NativeInit();
>        private static native void NativeResize(int w, int h);
>        private static native void NativeRender();
>        private static native void NativeDone();
>        private static native void NativeInitJavaCallbacks();
>
>        /*
>                Members
>        */
>
>        EGL10 m_EGL;
>        EGLDisplay m_EGLDisplay;
>        EGLSurface m_EGLSurface;
>        EGLConfig m_EGLConfig;
>        EGLContext m_EGLContext;
>
> }
>
> I try to call SwapBuffers from native code:
>
> #define YG_JAVA_PACKAGE_PATH com_example_YoghurtGum
> #define JAVA_EXPORT_NAME2(name, package) Java_##package##_##name
> #define JAVA_EXPORT_NAME1(name, package) JAVA_EXPORT_NAME2(name,
> package)
> #define JAVA_EXPORT_NAME(name) JAVA_EXPORT_NAME1(name,
> YG_JAVA_PACKAGE_PATH)
>
> #ifdef __cplusplus
>        #define C_LINKAGE "C"
> #else
>        #define C_LINKAGE
> #endif
>
> extern C_LINKAGE void
> JAVA_EXPORT_NAME(YGRenderer_NativeInitJavaCallbacks) (JNIEnv* env,
> jobject thiz)
> {
>        char classPath[1024];
>        JavaEnv = env;
>        JavaRenderer = thiz;
>
>        JavaRendererClass = JavaEnv->GetObjectClass(thiz);
>        JavaSwapBuffers = JavaEnv->GetMethodID(JavaRendererClass,
> "SwapBuffers", "()I");
> }
>
> This seems like it should work to me. My Java class has a method
> called "SwapBuffers", it takes 0 arguments and it returns an int.
> However, when I run it in the emulator, I get the following:
>
> I/dalvikvm(  809): Method not found: 'SwapBuffers' '()I' in Ljava/lang/
> Class;
> W/dalvikvm(  809): threadid=17: thread exiting with uncaught exception
> (group=0x4001aa28)
> E/AndroidRuntime(  809): Uncaught handler: thread GLThread exiting due
> to uncaught exception
> E/AndroidRuntime(  809): java.lang.NoSuchMethodError: SwapBuffers
> E/AndroidRuntime(  809):        at
> com.example.YoghurtGum.YGRenderer.NativeInitJavaCallbacks(Native
> Method)
> E/AndroidRuntime(  809):        at
> com.example.YoghurtGum.YGRenderer.onSurfaceCreated(YoghurtGum.java:
> 122)
> E/AndroidRuntime(  809):        at android.opengl.GLSurfaceView
> $GLThread.guardedRun(GLSurfaceView.java:946)
> E/AndroidRuntime(  809):        at android.opengl.GLSurfaceView
> $GLThread.run(GLSurfaceView.java:887)
>
> What am I doing wrong?
>
> Thanks in advance.
>
> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="17NcKJ4kbEkJ">andro...@....
> To unsubscribe from this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="17NcKJ4kbEkJ">android-ndk...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
>
>

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="17NcKJ4kbEkJ">andro...@....
To unsubscribe from this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="17NcKJ4kbEkJ">android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/android-ndk/-/6F9fTmtR8b4J.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.