未找到本机方法/运行时异常

时间:2015-03-09 16:20:50

标签: java android c++ android-ndk java-native-interface

我在Windows 8.1上使用Java-Eclipse Luna进行Android编程,我也在使用本机环境(C ++)。我得到一个“未找到本机方法”错误,我不知道背后的原因。这是我的代码:

我的.java文件:

epackage com.example.abstractnewocvpart3;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.LoaderCallbackInterface;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class AbstractNewOCVpart3Activity extends Activity {

float dataArray[] = new float[4];
float mat[] = new float[4];
private static final String    TAG = "OCVSample::Activity";
private CameraBridgeViewBase   mOpenCvCameraView;

public static native float[] MatFcn(float[] x);

private BaseLoaderCallback  mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");

                // Load native library after(!) OpenCV initialization
                System.loadLibrary("myLib");

                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
    };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_abstract_new_ocvpart3);

    dataArray[0] = 0;
    dataArray[1] = 1;
    dataArray[2] = 2;
    mat = MatFcn(dataArray);
    mat[0] = mat[0] + 1;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.abstract_new_ocvpart3, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

现在我的.cpp本机库:

#include <jni.h>
#include <opencv2/core/core_c.h>
#include <opencv2/core/types_c.h>

#include <opencv2/features2d/features2d.hpp>
#include <vector>

using namespace std;
using namespace cv;


extern "C" {
//static jfloatArray  MatFcn(JNIEnv *pEnv, jclass clazz, jfloatArray x);
 jfloatArray JNICALL MatFcn(JNIEnv *pEnv, jclass clazz, jfloatArray x)
{
jfloatArray jfloatArr = x;
jfloat *floatArr = pEnv->GetFloatArrayElements(jfloatArr, 0);
pEnv->ReleaseFloatArrayElements(jfloatArr, floatArr, 0);
return jfloatArr;

}
}

/** The bossy JNI_OnLoad() method */
      jint  JNI_OnLoad(JavaVM* vm, void* reserved) // this method is called when the native library is loaded
{
    JNIEnv* env;
    if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK){
        return -1;
    } // end of if()

    JNINativeMethod nm[1];
    nm[0].name = "MatFcn";
    nm[0].signature = "([F)[F";
    nm[0].fnPtr = (void *) MatFcn;

    jclass cls = env->FindClass("com/example/abstractnewocvpart3/AbstractNewOCVpart3Activity");
    //Register methods with env->RegisterNatives. Methods that you create. methods registration
    //should be done in the JNI_OnLoad() method to guarantee that native methods are registered
    //before being invoked.
    env->RegisterNatives(cls, nm, 1);
    env->DeleteLocalRef(cls);
    return JNI_VERSION_1_6;

}// end of JNI_OnLoad()

在运行时,我在Logcat中遇到一个异常,说未找到本机方法。

提前致谢

0 个答案:

没有答案