gpt4 book ai didi

java - NDK 与 JNI 同时调用返回类型为非原始类型的方法

转载 作者:太空宇宙 更新时间:2023-11-04 02:51:44 24 4
gpt4 key购买 nike

我需要 c 结构和 java 类模型之间的映射。原因是我想调用一个 c 的方法,它返回一个结构,我在 java 中有模型类。我已经成功地从 Java 代码调用了原始数据类型或 void 的本地方法。但对于我的案例 AlacFile 或只是 Object obj 没有任何对象。看我所说的代码。

C 代码...

alac_file * Java_org_phlo_AirReceiver_AirReceiver_create_alac2(JNIEnv* env,
jclass thiz, int samplesize, int numchannels) {

return create_alac(samplesize, numchannels);
}

alac_file *create_alac(int samplesize, int numchannels){
alac_file *newfile = (alac_file*)malloc(sizeof(alac_file));

newfile->samplesize = samplesize;
newfile->numchannels = numchannels;
newfile->bytespersample = (samplesize / 8) * numchannels;

return newfile;
}

我有一个在 c 类中定义的 alac_file 结构。

我正在从 java 中调用此方法,如下所示。

public native AlacFile create_alac2(int samplesize , int numchannels );

输出;

编译成功并运行。

调用上述 native 方法时发生崩溃。崩溃如下。

    01-28 03:51:56.202: I/AirReceiver(31668): Registered AirTunes service           '08606EB2F10B@localhost (p2p0)' on fe80::860:6eff:feb2:f10b%p2p0/fe80::860:6eff:feb2:f10b%p2p0%5
01-28 03:51:56.212: I/System.out(31668): Loading libraray BEFORE 1111
01-28 03:51:56.212: D/dalvikvm(31668): Trying to load lib /data/app-lib/com.example.airreceiver-1/libhairtunes.so 0x4212dad0
01-28 03:51:56.232: D/dalvikvm(31668): Added shared lib /data/app-lib/com.example.airreceiver-1/libhairtunes.so 0x4212dad0
01-28 03:51:56.232: D/dalvikvm(31668): No JNI_OnLoad found in /data/app-lib/com.example.airreceiver-1/libhairtunes.so 0x4212dad0, skipping init
01-28 03:51:56.232: I/System.out(31668): Loading libraray AFTER 1111
01-28 03:51:56.232: W/dalvikvm(31668): No implementation found for native Lorg/phlo/AirReceiver/AirReceiver;.create_alac2:(II)Lcom/beatofthedrum/alacdecoder/AlacFile;
01-28 03:51:56.242: W/dalvikvm(31668): threadid=11: thread exiting with uncaught exception (group=0x419d6700)
01-28 03:51:56.252: E/AndroidRuntime(31668): FATAL EXCEPTION: AsyncTask #1
01-28 03:51:56.252: E/AndroidRuntime(31668): java.lang.RuntimeException: An error occured while executing doInBackground()
01-28 03:51:56.252: E/AndroidRuntime(31668): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-28 03:51:56.252: E/AndroidRuntime(31668): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-28 03:51:56.252: E/AndroidRuntime(31668): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-28 03:51:56.252: E/AndroidRuntime(31668): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-28 03:51:56.252: E/AndroidRuntime(31668): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-28 03:51:56.252: E/AndroidRuntime(31668): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-28 03:51:56.252: E/AndroidRuntime(31668): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-28 03:51:56.252: E/AndroidRuntime(31668): at java.lang.Thread.run(Thread.java:841)
01-28 03:51:56.252: E/AndroidRuntime(31668): Caused by: java.lang.UnsatisfiedLinkError: Native method not found: org.phlo.AirReceiver.AirReceiver.create_alac2:(II)Lcom/beatofthedrum/alacdecoder/AlacFile;
01-28 03:51:56.252: E/AndroidRuntime(31668): at org.phlo.AirReceiver.AirReceiver.create_alac2(Native Method)
01-28 03:51:56.252: E/AndroidRuntime(31668): at org.phlo.AirReceiver.AirReceiver.main(AirReceiver.java:461)
01-28 03:51:56.252: E/AndroidRuntime(31668): at com.example.droidairplayandroid.MainActivity$AirServerAsyncTask.doInBackground(MainActivity.java:66)
01-28 03:51:56.252: E/AndroidRuntime(31668): at com.example.droidairplayandroid.MainActivity$AirServerAsyncTask.doInBackground(MainActivity.java:1)
01-28 03:51:56.252: E/AndroidRuntime(31668): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-28 03:51:56.252: E/AndroidRuntime(31668): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-28 03:51:56.252: E/AndroidRuntime(31668): ... 4 more
01-28 03:51:56.282: I/Process(31668): Sending signal. PID: 31668 SIG: 9

最佳答案

您必须定义您的函数以返回jobject。使用 this example在 C 中创建 Java 对象的新实例,填充它,然后返回给 Java。

关于java - NDK 与 JNI 同时调用返回类型为非原始类型的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21413158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com