我需要c结构和java类模型之间的映射。原因是我想调用一个c的方法,它返回一个我在java中有模型类的结构。我已成功调用原始数据类型的本机方法或从Java代码中删除。但不是我的案例AlacFile的任何对象或只是对象obj。 看看我说的代码。
c code ...
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 );
输出;
成功编译并运行。
当上面的本机方法调用时收到崩溃。崩溃如下。
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
答案 0 :(得分:2)
您必须定义函数以返回 jobject 。使用this example在C中创建一个新的Java对象实例,填充它并返回Java。
答案 1 :(得分:0)
可能是你的方法签名是关心问题。
public native AlacFile create_alac2(int samplesize , int numchannels );
尝试一次,
public native AlacFile createAlac2(int samplesize , int numchannels );//remove '-' here
可能是它的分裂创造和alac2。