android ndk null,使用android ndk'NULL'未聲明(在此函數中首次使用)

I am working on android-ndk in which i try to pass string from my C code to my java and then in activity. My codes are given below but i am getting the following errors. Please help me out here

我正在研究android-ndk,其中我嘗試將字符串從我的C代碼傳遞到我的java然后在活動中。我的代碼如下,但我收到以下錯誤。請幫幫我

#include

JNIEXPORT jstring JNICALL Java_Sample1_stringMethod(JNIEnv *env, jobject obj,

jstring string) {

const char *name = (*env)->GetStringUTFChars(string, NULL); //Java String to C Style string

char msg[60] = "HelloWorld";

jstring result;

strcat(msg, name);

(*env)->ReleaseStringUTFChars(string, name);

puts(msg);

result = (*env)->NewStringUTF(msg); // C style string to Java String

return result;

}

Error log:

jni/com_testing_ndk_FibLib.c: In function 'Java_Sample1_stringMethod':

jni/com_testing_ndk_FibLib.c:7:55: error: 'NULL' undeclared (first use in this function)

const char *name = (*env)->GetStringUTFChars(string, NULL); //Java String to C Style string

^

jni/com_testing_ndk_FibLib.c:7:55: note: each undeclared identifier is reported only once for each function it appears in

jni/com_testing_ndk_FibLib.c:7:21: error: too few arguments to function '(*env)->GetStringUTFChars'

const char *name = (*env)->GetStringUTFChars(string, NULL); //Java String to C Style string

^

jni/com_testing_ndk_FibLib.c:11:2: warning: incompatible implicit declaration of built-in function 'strcat'

strcat(msg, name);

^

jni/com_testing_ndk_FibLib.c:12:2: warning: passing argument 2 of '(*env)->ReleaseStringUTFChars' discards 'const' qualifier from pointer target type

(*env)->ReleaseStringUTFChars(string, name);

^

jni/com_testing_ndk_FibLib.c:12:2: note: expected 'jstring' but argument is of type 'const char *'

jni/com_testing_ndk_FibLib.c:12:2: error: too few arguments to function '(*env)->ReleaseStringUTFChars'

jni/com_testing_ndk_FibLib.c:14:11: warning: passing argument 1 of '(*env)->NewStringUTF' from incompatible pointer type

result = (*env)->NewStringUTF(msg); // C style string to Java String

^

jni/com_testing_ndk_FibLib.c:14:11: note: expected 'const struct JNINativeInterface **' but argument is of type 'char *'

jni/com_testing_ndk_FibLib.c:14:11: error: too few arguments to function '(*env)->NewStringUTF'

make: *** [obj/local/arm64-v8a/objs/com_testing_ndk_FibLib/com_testing_ndk_FibLib.o] Error 1

My Java Code:

我的Java代碼:

public class FibLib {

static {

System.loadLibrary("com_testing_ndk_FibLib");

}

public static native String stringMethod(String text);

public static void main(String[] args)

{

System.loadLibrary("Sample1");

FibLib sample = new FibLib();

String text = sample.stringMethod("world");

System.out.println("stringMethod: " + text);

}

}

2 个解决方案

#1

All your JNI calls are missing the first parameter which should be env, as you're in compiling C code.

當您編譯C代碼時,所有JNI調用都缺少第一個應該是env的參數。

If your code was in C++, you could make calls like env->NewStringUTF("xx"). But in C, JNIEnv* isn't an object, hence you have to pass it as first argument, like so: (*env)->NewStringUTF(env, "xx")

如果您的代碼是C ++,您可以調用env-> NewStringUTF(“xx”)。但是在C中,JNIEnv *不是一個對象,因此你必須將它作為第一個參數傳遞,如下所示:(* env) - > NewStringUTF(env,“xx”)

As for NULL being undefined, you can solve this by including the header that defines it: #include

對於未定義的NULL,您可以通過包含定義它的頭來解決此問題:#include

#2

The second parameter does not seem to be optional:

第二個參數似乎不是可選的:

jboolean iscopy;

const char *name = (*env)->GetStringUTFChars(string, &iscopy);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值