作者Zichi (外表是最好的掩饰)
看板AndroidDev
标题[问题]cannot call member function without obj
时间Thu Oct 24 18:55:41 2013
请教大家关於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
1F:→ hopesong:setItemValue不是static function 改成static 试看看 10/25 00:10
3F:→ Zichi:感谢大大,但在.h加上static错误讯息变undefined reference 10/25 10:03
4F:→ Zichi:to `android::TestClient::setItemValue(int, int)' 10/25 10:03
※ 编辑: Zichi 来自: 59.124.185.194 (10/25 11:34)
5F:→ hopesong:undefined reference是c++常见问题 google很容易找答案 10/25 23:14
6F:→ hopesong:通常是namespace或是可见范围或名称不match之类的 10/25 23:15