leitongzhuang1 2015-10-26 10:49 采纳率: 0%
浏览 2218
已结题

anroid jni回调java函数问题

JAVA 代码
1.NativeProviefder.java

package com.example.test;

public class NativeProviefder {

static {
    System.loadLibrary("test_jni");
}
public native void nativeInitilize();
public native void start();
public native void stop();
public void onNativeCallback(int count) {
    System.out.println("onNativeCallback count=" + count);
}
}

2.MainActivity.java
package com.example.test;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
    NativeProviefder np = new NativeProviefder();
    boolean isstart = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        np.nativeInitilize();
        ((Button) findViewById(R.id.btnstart)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (isstart) {
                    np.stop();
                } else {
                    np.start();
                }
            }
        });
    }
...
}
 JNI代码
test_jni.c
    #include     <stdio.h>
    #include     <stdlib.h>
    #include     <unistd.h>
    #include     <sys/types.h>
    #include     <sys/stat.h>
    #include     <fcntl.h>
    #include     <termios.h>
    #include     <errno.h>
    #include     <string.h>
    #include     <android/log.h>
    #include     <jni.h>
    #include     <pthread.h>

    #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "", __VA_ARGS__));

    JavaVM* gJavaVM;
    jobject gJavaObj;
    static volatile int gExit = 0;

    static void* native_thread_exec(void* arg) {
        JNIEnv *env;
        int status;

        LOGI("onNativeCallback thread1");
        status = (*gJavaVM)->AttachCurrentThread(gJavaVM, &env, NULL);
        if (status < 0) {
            LOGI("onNativeCallback  modem_log, Failed to GetObjectClass");
            return 0;
        }
        LOGI("onNativeCallback  thread2");
        jclass javaClass = (*env)->GetObjectClass(env, gJavaObj);
        if (javaClass == NULL) {
            LOGI("onNativeCallback Failed to find javaClass");
            return 0;
        }
        LOGI("onNativeCallback  thread3 javaClass (I)V");
        jmethodID javaCallback = (*env)->GetMethodID(env, javaClass, "onNativeCallback", "(I)V"); //这一句报错,提示:java.lang.NoSuchMethodError: no non-static method "Lcom/example/test/NativeProviefder;.onNativeCallback(I)V"
        if (javaCallback == NULL) {
            LOGI("onNativeCallback  modem_log, Failed to find method javaCallback");
            (*gJavaVM)->DetachCurrentThread(gJavaVM);
            return 0;
        }

        int count = 0;
        while (!gExit) {
            (*env)->CallVoidMethod(env, gJavaObj, javaCallback, count++);
            sleep(1);
        }
        (*gJavaVM)->DetachCurrentThread(gJavaVM);
        return 1;
    }

    JNIEXPORT void JNICALL Java_com_example_test_NativeProviefder_nativeInitilize(JNIEnv * env, jobject thiz) {
        (*env)->GetJavaVM(env, &gJavaVM);
        gJavaObj = (*env)->NewGlobalRef(env, thiz);
    }

    JNIEXPORT void JNICALL Java_com_example_test_NativeProviefder_start(JNIEnv * env, jobject thiz) {
        pthread_t threadId;
        if (pthread_create(&threadId, NULL, native_thread_exec, NULL) != 0) {
            LOGI("onNativeCallback  start faild!");
            return;
        }
        LOGI("onNativeCallback  start success");
    }
    JNIEXPORT void JNICALL Java_com_example_test_NativeProviefder_stop(JNIEnv * env, jobject thiz) {
        gExit = 1;
        LOGI("onNativeCallback  stop!");
    }

Jni的Android.mk
    LOCAL_PATH := $(call my-dir)

    include $(CLEAR_VARS)

    LOCAL_SRC_FILES := test_jni.c
    LOCAL_C_INCLUDES := $(JNI_H_INCLUDE) $(LOCAL_PATH) $(MTK_ROOT)/external/meta/common/inc
    LOCAL_MODULE := libtest_jni
    LOCAL_SHARED_LIBRARIES := libutils libcutils
    #LOCAL_PRELINK_MODULE := true
    LOCAL_MODULE_TAGS := optional eng
    LOCAL_PROGUARD_ENABLED:= disabled

    include $(BUILD_SHARED_LIBRARY)

错误找不到JAVA里的函数,不知道什么原因,大神们,请指教?
jmethodID javaCallback = (*env)->GetMethodID(env, javaClass, "onNativeCallback", "(I)V"); //这一句报错,提示:java.lang.NoSuchMethodError: no non-static method "Lcom/example/test/NativeProviefder;.onNativeCallback(I)V"

补充:是在android5.1上运行。
  • 写回答

2条回答 默认 最新

  • devmiao 2015-10-26 15:56
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀