zoukankan      html  css  js  c++  java
  • Jni的一个异常

      UnsatisfiedLinkError:No implementation found for java.lang.String com.skymaster.hs.test4.MainActivity.getstringFromJNI();

    就是桌这个路径下的方法stringFromJNI();没有被实现,看看全部代码

    public class MainActivity extends AppCompatActivity {
    
        // Used to load the 'native-lib' library on application startup.
        static {
            System.loadLibrary("native-lib");
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Example of a call to a native method
            TextView tv = (TextView) findViewById(R.id.sample_text);
            tv.setText(stringFromJNI());
            tv.append(getStringFromJNI());
        }
    
        /**
         * A native method that is implemented by the 'native-lib' native library,
         * which is packaged with this application.
         */
        public native String stringFromJNI();
        public native String getStringFromJNI();
    }

    下面是cpp目录中native-lib.cpp的代码

    #include <jni.h>
    #include <string>
    using namespace std;
    extern "C"
    
        jstring
        Java_com_skymaster_hs_test4_MainActivity_stringFromJNI(
                JNIEnv *env,
                jobject /* this */) {
            std::string hello = "Hello from C++";
            return env->NewStringUTF(hello.c_str());
        }
    
        jstring
        Java_com_skymaster_hs_test4_MainActivity_getStringFromJNI(
                JNIEnv *env,
                jobject /* this */) {
            string hello = "    Hello Android";
            return env->NewStringUTF(hello.c_str());
        }

    这里虽然是c++的源文件,但是两个函数都是c的函数,所以需要用extern "C"(大写的C)来告诉c++编译器需要按照c的的方式来编译与链接。问题出在
    extern "C"的作用范围,如果不用大括号包括2个函数就会出问题。修改如下就好了

    #include <jni.h>
    #include <string>
    using namespace std;
    extern "C" {
    
        jstring
        Java_com_skymaster_hs_test4_MainActivity_stringFromJNI(
                JNIEnv *env,
                jobject /* this */) {
            std::string hello = "Hello from C++";
            return env->NewStringUTF(hello.c_str());
        }
    
        jstring
        Java_com_skymaster_hs_test4_MainActivity_getStringFromJNI(
                JNIEnv *env,
                jobject /* this */) {
            string hello = "    Hello Android";
            return env->NewStringUTF(hello.c_str());
        }
    }
    这个只不过是自己的流水账,偶尔有一些心得,错误的地方概不负责
  • 相关阅读:
    activity(工作流)初步学习记录
    npm install出现npm ERR! write after end解决方法
    npm ERR! Cannot read property 'match' of undefined 错误处理
    gist.github.com 被墙无法访问解决办法
    AS SSD Benchmark固态硬盘检测工具
    VSCode 云同步扩展设置 Settings Sync 插件
    window系统不显示预览图片的处理方法
    VsCode如何同时打开多个项目
    win10的开机启动文件夹在哪及开机自动启动软件
    Java环境配置和tomcat环境配置
  • 原文地址:https://www.cnblogs.com/ashitaka/p/5947026.html
Copyright © 2011-2022 走看看