base operand of '->' has non-pointer type 'JNIEnv {aka _JNIEnv}'和Method 'GetStringUTFChars' could no

來源:互聯網
上載者:User

標籤:

Android NDK帶的jni例子都是使用C定義JNI介面,但是在項目中,因為Native代碼是用C++編寫的,所以我就使用C++定義JNI介面,但是初學者總會遇到很多問題:


jni中的常見問題:

1、base operand of ‘->‘ has non-pointer type ‘JNIEnv {aka _JNIEnv}‘和Method ‘GetStringUTFChars‘ could not be resolved

其中這個問題是跟你jni配置時到底是.cpp還是.c檔案及:

和      


解決問題的辦法:

1》使用c++來寫代碼,檔案名稱就必須【cpp】尾碼: C++ code must have .cpp extension.,必須cpp尾碼,c尾碼不行;2》使用c來寫代碼,檔案名稱就必須【c】尾碼;
2》調用的代碼這麼來寫://return (*env)->NewStringUTF(env, "Hello from JNI !");//如果是用C語言格式就用這種方式
//return env->NewStringUTF((char *)"Hello from JNI !");//C++用這種格式


錯誤的辦法:

網上有一種方法是如下,但這方方法明顯是騙小孩子的,哪有用忽略這種東西的,不推薦


正確的辦法:

問題是就是上面第二點,因此



這是如果想深究原因可以查看<jni.h>

1、用.cpp檔案定義的

    const char* GetStringUTFChars(jstring string, jboolean* isCopy)
    { return functions->GetStringUTFChars(this, string, isCopy); }


2、用.c檔案定義

const char* (*GetStringUTFChars)(JNIEnv*, jstring, jboolean*);


base operand of '->' has non-pointer type 'JNIEnv {aka _JNIEnv}'和Method 'GetStringUTFChars' could no

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.