看板 AndroidDev 關於我們 聯絡資訊
請教大家關於JNI呼叫C++ function compile 錯誤 error: cannot call member function 'void android::TestClient::setItemValue(int, int)' without object 謝謝 以下是JNI的全部程式碼 android_media_TestService.cpp #define LOG_TAG "TestService-JNI" #include <utils/misc.h> #include <utils/Log.h> #include <stdio.h> #include <nativehelper/JNIHelp.h> #include <android_runtime/AndroidRuntime.h> #include "android_media_TestService.h" #include <media/TestClient.h> using namespace android; static void android_media_MyManager_setItemValue(JNIEnv *env, jobject thiz, jint item, jint value) { TestClient::setItemValue(item, value); //呼叫TestClient類別的setItemValue } static JNINativeMethod gMethods[] = { {"setItemValue", "(II)V", (void *)android_media_MyManager_setItemValue}, }; const char* const kClassPathName = "android/media/MyManager"; int register_android_media_MyManager(JNIEnv *env) { return AndroidRuntime::registerNativeMethods(env, "android/media/MyManager", gMethods, NELEM(gMethods)); } jint JNI_OnLoad(JavaVM* vm, void* reserved) { JNIEnv* env = NULL; jint result = -1; if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { goto bail; } assert(env != NULL); if (register_android_media_MyManager(env) < 0) { goto bail; } result = JNI_VERSION_1_4; bail: return result; } ======================================================================= TestClient.cpp 的程式碼 #include <binder/IServiceManager.h> #include <binder/IPCThreadState.h> #include <utils/Log.h> #include <media/TestClient.h> namespace android { sp<IBinder> binder; void TestClient::add100(int n){ getTestService(); Parcel data, reply; int answer; data.writeInt32(getpid()); data.writeInt32(n); binder->transact(0, data, &reply); answer = reply.readInt32(); return; } const void TestClient::getTestService(){ sp<IServiceManager> sm = defaultServiceManager(); binder = sm->getService(String16("Vince.TestService")); if (binder == 0){ LOGW("TestService not published, waiting..."); return; } } void TestClient::setItemValue(int item, int value){ LOGE("TestClient::setItemValue item:%d, value:%d\n", item, value); //先試著呼叫到這邊 } }; //namespace using namespace android; int main(int argc, char** argv) { TestClient* p = new TestClient(); p->add100(1); return 0; } ============================================================================ TestClient.h namespace android { class TestClient { public: void add100(int n); void setItemValue(int item, int value); private: static const void getTestService(); }; }; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.124.185.194
hopesong:setItemValue不是static function 改成static 試看看 10/25 00:10
hopesong:http://ppt.cc/Kkqu 10/25 00:11
Zichi:感謝大大,但在.h加上static錯誤訊息變undefined reference 10/25 10:03
Zichi:to `android::TestClient::setItemValue(int, int)' 10/25 10:03
※ 編輯: Zichi 來自: 59.124.185.194 (10/25 11:34)
hopesong:undefined reference是c++常見問題 google很容易找答案 10/25 23:14
hopesong:通常是namespace或是可見範圍或名稱不match之類的 10/25 23:15