android c 调用java类_在Android中从c调用java方法

我试图从C获得一个简单的Java方法调用,而Java调用本地方法。这里是Java代码:

public class MainActivity extends Activity {

private static String LIB_NAME = "name";

static {

System.loadLibrary(LIB_NAME);

}

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TextView tv = (TextView) findViewById(R.id.textview);

tv.setText(this.getJniString());

}

public void messageMe(String text) {

System.out.println(text);

}

public native String getJniString();

}

我试图调用messageMe方法从本地代码的过程中的getJniString *方法调用从Java到本机。

native.cpp:

#include

#include

#include

jstring Java_the_package_MainActivity_getJniString( JNIEnv* env, jobject obj, jint depth ){

// JavaVM *vm;

// JNIEnv *env;

// JavaVMInitArgs vm_args;

// vm_args.version = JNI_VERSION_1_2;

// vm_args.nOptions = 0;

// vm_args.ignoreUnrecognized = 1;

//

// // Construct a VM

// jint res = JNI_CreateJavaVM(&vm, (void **)&env, &vm_args);

// Construct a String

jstring jstr = env->NewStringUTF("This string comes from JNI");

// First get the class that contains the method you need to call

jclass clazz = env->FindClass("the/package/MainActivity");

// Get the method that you want to call

jmethodID messageMe = env->GetMethodID(clazz, "messageMe", "(Ljava/lang/String;)V");

// Call the method on the object

jobject result = env->CallObjectMethod(jstr, messageMe);

// Get a C-style string

const char* str = env->GetStringUTFChars((jstring) result, NULL);

printf("%s\n", str);

// Clean up

env->ReleaseStringUTFChars(jstr, str);

// // Shutdown the VM.

// vm->DestroyJavaVM();

return env->NewStringUTF("Hello from JNI!");

}

在干净的编译应用程序停止与下一条消息:

ERROR/AndroidRuntime(742): FATAL EXCEPTION: main

java.lang.NoSuchMethodError: messageMe

at *.android.t3d.MainActivity.getJniString(Native Method)

at *.android.t3d.MainActivity.onCreate(MainActivity.java:22)

显然,这意味着方法名称是错误的,但它看起来不错。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值