我正在尝试使用 ndk 从 android 代码调用本机函数,但遇到了一些问题。该应用程序将运行一小段时间,然后出现一个错误,指示“已意外停止”。Logcat 显示这是因为 UnsatisfiedLinkError: surfaceCreated。
日志猫:
02-12 00:09:33.918: E/AndroidRuntime(25900): FATAL EXCEPTION: GLThread 10
02-12 00:09:33.918: E/AndroidRuntime(25900): java.lang.UnsatisfiedLinkError: surfaceCreated
02-12 00:09:33.918: E/AndroidRuntime(25900): at com.example.lesnaH.gameengine.GameRenderer.surfaceCreated(Native Method)
02-12 00:09:33.918: E/AndroidRuntime(25900): at com.example.lesnaH.gameengine.GameRenderer.onSurfaceCreated(GameSurfaceView.java:23)
02-12 00:09:33.918: E/AndroidRuntime(25900): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1373)
02-12 00:09:33.918: E/AndroidRuntime(25900): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1138)
这是我试图从 Java 调用函数的地方:
package com.example.lesnaH.gameengine;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLSurfaceView;
public class GameSurfaceView extends GLSurfaceView {
private GameRenderer renderer;
public GameSurfaceView(Context context)
{
super(context);
renderer = new GameRenderer();
setRenderer(renderer);
}
}
class GameRenderer implements GLSurfaceView.Renderer {
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
surfaceCreated();
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
surfaceChanged(width, height);
}
public void onDrawFrame(GL10 gl) {
drawFrame();
}
public native void surfaceCreated();
public native void surfaceChanged(int w, int h);
public native void drawFrame();
}
以下是这些函数在 C++ 中的实现:
#include <jni.h>
void initializeOpenGL();
void resizeViewport(int newWidth, int newHeight);
void renderFrame();
JNIEXPORT void JNICALL Java_com_example_lesnaH_gameengine_GameRenderer_surfaceCreated(JNIEnv*, jobject)
{
initializeOpenGL();
}
JNIEXPORT void JNICALL Java_com_example_lesnaH_gameengine_GameRenderer_surfaceChanged(JNIEnv*, jobject, jint width, jint height)
{
resizeViewport(width, height);
}
JNIEXPORT void JNICALL Java_com_example_lesnaH_gameengine_GameRenderer_drawFrame(JNIEnv*, jobject)
{
renderFrame();
}
这是我的 Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := gameengine
LOCAL_SRC_FILES := graphics.cc \
graphicsExports.cc
LOCAL_LDLIBS := -lGLESv2
include $(BUILD_SHARED_LIBRARY)
如果有人希望我发布其他任何内容/提供更多信息,我很乐意这样做。非常感谢您能给我的任何帮助!