JNI NewStringUTF调用挂起异常'java.lang.NoSuchMethodError'

时间:2015-11-12 15:07:39

标签: java android android-ndk

我不知道为什么在调用native方法时抛出异常。

原生代码:

JNIEXPORT jstring JNICALL Java_org_amk_lockscreen_JNIActivity_getPackege(JNIEnv *env,jclass cls,jobject context){
    jclass Context_class = (*env).FindClass("android/content/Context");
    jmethodID midGetPackageName = (*env).GetMethodID(Context_class, "getPackageName", "(V)Ljava/lang/String;");
    if(midGetPackageName==0){
        return (*env).NewStringUTF("Not Found Method");
    }
    else{
        jobject packageName=(*env).CallObjectMethod( context, midGetPackageName);
        return (jstring)packageName;
    }
}

和我的java代码:

public class JNIActivity extends Activity {

    static {
        System.loadLibrary("myjni"); // "myjni.dll" in Windows, "libmyjni.so" in Unixes
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jni);
        TextView txt = (TextView) findViewById(R.id.txtMessage);
        try {
            txt.setText(getPackege(getApplicationContext()));
        } catch (Exception exp) {
            txt.setText(exp.getMessage());
        }
    }

    public native String getMessage();

    public native String getPackege(Context con);
}

但是当运行它时,它会抛出异常:

JNI DETECTED ERROR IN APPLICATION: JNI NewStringUTF called with pending exception 'java.lang.NoSuchMethodError' thrown in java.lang.String org.amk.lockscreen.JNIActivity.getPackege(android.content.Context):-2
in call to NewStringUTF
from java.lang.String org.amk.lockscreen.JNIActivity.getPackege(android.content.Context)

我测试了不同的方法签名,例如:

(Ljava/lang/Void;)Ljava/lang/String;     
   (V;)Ljava/lang/String;

但它再次抛出异常。

我犯了错误?

2 个答案:

答案 0 :(得分:4)

GetMethodID引发异常。您没有检查是否使用env-> ExceptionCheck(env)引发异常,然后使用ExceptionClear()将其清除,或者立即从函数返回,以便JVM处理它。请在此处查看有关getMethodId的Oracle文档:http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html

例如(来自http://www.ibm.com/developerworks/library/j-jni/ :) findClass,GetStaticMethodID和CallStaticVoidMethod调用都生成类似于GetMethodID的异常。

int val=1;
jmethodID method;
jclass cls;

cls = (*env)->FindClass(env, "com/ibm/example/TestClass");
if ((*env)->ExceptionCheck(env)) {
   return ERR_FIND_CLASS_FAILED;
}
method = (*env)->GetStaticMethodID(env, cls, "setInfo", "(I)V");
if ((*env)->ExceptionCheck(env)) {
   return ERR_GET_STATIC_METHOD_FAILED;
}
(*env)->CallStaticVoidMethod(env, cls, method,val);
if ((*env)->ExceptionCheck(env)) {
   return ERR_CALL_STATIC_METHOD_FAILED;
}

答案 1 :(得分:2)

你的方法定义在这里是错误的:

jmethodID midGetPackageName = (*env).GetMethodID(Context_class, "getPackageName", "Ljava/lang/String;(V)");

您根本无法在参数中传递(V),因为这意味着您传递的是void参数,这在Java中没有意义。您需要确定将哪些参数类型传递给您的java方法(例如 String ),以及此方法返回的返回类型(例如 void )。

因此,您可能打算做这样的事情:

CREATE TABLE #SOURCE ( a int NOT NULL, b int NOT NULL, c int NOT NULL, ); INSERT INTO #SOURCE (a, b, c) VALUES (1, 2, 3), (1, 3, 4), --either this record or the one below should be deleted (1, 3, 6), (3, 4, 5), (3, 5, 6), (5, 6, 7), (5, 7, 8), --either this record or the one below should be deleted (5, 7, 0) (6, 8, 9); CREATE TABLE #TARGET ( a int NOT NULL, b int NOT NULL, c int NOT NULL, CONSTRAINT pk PRIMARY KEY CLUSTERED (a, b) );