简体   繁体   English

Android本机方法对Java不可见

[英]Android native method is invisible for Java

I am struggling with very annoying error in my code. 我在代码中遇到非常烦人的错误。 I have this error appearing over and over again: 我一次又一次出现此错误:

    No implementation found for long com.oculus.gles3jni.GLES3JNILib.onCreate(android.app.Activity) 
(tried Java_com_oculus_gles3jni_GLES3JNILib_onCreate and 
Java_com_oculus_gles3jni_GLES3JNILib_onCreate__Landroid_app_Activity_2)

But in my file GLES3JNILib.java I have this: 但是在我的文件GLES3JNILib.java中,我有这个:

package com.oculus.gles3jni;

import android.app.Activity;
import android.view.Surface;

// Wrapper for native library

public class GLES3JNILib
{
    // Activity lifecycle
    public static native long onCreate( Activity obj );
    public static native void onStart( long handle );
    public static native void onResume( long handle );
    public static native void onPause( long handle );
    public static native void onStop( long handle );
    public static native void onDestroy( long handle );

    // Surface lifecycle
    public static native void onSurfaceCreated( long handle, Surface s );
    public static native void onSurfaceChanged( long handle, Surface s );
    public static native void onSurfaceDestroyed( long handle );

    // Input
    public static native void onKeyEvent( long handle, int keyCode, int action );
    public static native void onTouchEvent( long handle, int action, float x, float y );
}

So I am not sure what is wrong. 所以我不确定是什么问题。 It is there but still I can't start my app. 它在那里,但仍然无法启动我的应用程序。 In my cpp code the implementation is: 在我的cpp代码中,实现为:

jlong Java_com_oculus_gles3jni_GLES3JNILib_onCreate( JNIEnv * env, jobject obj, jobject activity )
{
...
}

Does someone see what I am missing, or doing wrong? 有人看到我的缺失或做错了吗? Is it possible that this is because I don't have h file for my cpp? 这是否可能是因为我的cpp没有h文件?

You must have generated the C code and then changed the Java native method declaration to static without re-running javah . 您必须已经生成了C代码,然后将Java本机方法声明更改为static而不重新运行javah

Or you didn't run it at all and tried to wing it. 或者,您根本不运行它,而是尝试对其进行控制。 Don't do that. 不要那样做

The correct signature has jclass for the second parameter, but don't take my word for it: rerun javah and adjust your .c file accordingly. 正确的签名具有用于第二个参数的jclass ,但请不要jclass :重新运行javah并相应地调整.c文件。

NB your .c file should #include your .h file. 注意:您的.c文件应#include您的.h文件。

Did you forget to load the library? 您忘了加载图书馆吗?

public class GLES3JNILib
{
   static {
      try {
        System.loadLibrary("libGLES3JNILib");
      } catch (UnsatisfiedLinkError e) {
          // do something helpful here
      }
  }

  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM