我在一个简单的 android 应用程序中有一个按钮,用于调用本机函数。主活动中的按钮调用 HADriver.java 的一个函数。该函数依次调用 DriverAdapter.cpp 中的 JNI 函数。然后,该 JNI 函数依次调用 Driver.cpp 中的本机函数。testCout
以下是在单击按钮时发挥作用的每个文件的部分MainActivity
。
MainActivity 中的按钮:
private HADriver driver = new HADriver();
....
testCout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
driver.testCout();
}
});
HADriver.java
package com.ihearhtpi;
public class HADriver {
static {
System.loadLibrary("gnustl_shared");
System.loadLibrary("driveradapter");
}
public native void testCout();
}
驱动适配器.cpp
#include <jni.h>
#include <string.h>
#include <android/log.h>
#include <Driver/driver.h>
#define DEBUG_TAG "NativeCalls"
void Java_com_ihearhtpi_HADriver_testCout(JNIEnv * env, jobject thiz)
{
Driver* driver = new Driver();
driver->testCoutFunc();
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", "Testing... this best effing work!");
delete driver;
}
驱动程序.cpp
#include <Driver/driver.h>
#include <iostream>
...
//bunch of custom irrelevant functions and includes
Driver::Driver()
{
CustomFuncs::initBitMasks(); // call global namespace function of CustomFuncs
}
void Driver::testCoutFunc()
{
std::cout << "These are the droids you're looking for." << std::endl;
}
但是,我不断收到以下错误,每次单击testCout
按钮时我的应用程序都会崩溃。
04-13 02:22:46.130: W/dalvikvm(17228): No implementation found for native Lcom/ihearhtpi/HADriver;.testCout:()V
04-13 02:22:46.130: D/AndroidRuntime(17228): Shutting down VM
04-13 02:22:46.130: W/dalvikvm(17228): threadid=1: thread exiting with uncaught exception (group=0x40e0c300)
04-13 02:22:46.130: E/AndroidRuntime(17228): FATAL EXCEPTION: main
04-13 02:22:46.130: E/AndroidRuntime(17228): java.lang.UnsatisfiedLinkError: Native method not found: com.ihearhtpi.HADriver.testCout:()V
04-13 02:22:46.130: E/AndroidRuntime(17228): at com.ihearhtpi.HADriver.testCout(Native Method)
是什么赋予了?我不明白为什么这个本机调用不起作用!