Pan*_*anu 2 java arrays java-native-interface android
我试图通过根据下面的代码设置字节数组参数来通过 JNI 调用 java 方法。
extern "C"
JNIEXPORT void JNICALL
Java_test_example_test_MainActivity_testOnProgress(JNIEnv *env, jobject instance,
jobject callback) {
//declare ref java class
jclass jClassTestCallBack = env->GetObjectClass(callback);
//declare java method id
jmethodID jMethodIdOnProgress = env->GetMethodID(jClassTestCallBack,"onProgress","([B)V");
//check null
if(jMethodIdOnProgress == 0){
return;
}
jbyteArray result = env->NewByteArray(10);
env->CallVoidMethod(callback,jMethodIdOnProgress,result);
}
Run Code Online (Sandbox Code Playgroud)
在java中。
public class MainActivity extends AppCompatActivity {
private final String TAG = getClass().getSimpleName();
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native void testOnSuccess(TestCallback callback);
public native void testOnProgress(TestCallback callback);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
final TextView tv = findViewById(R.id.sample_text);
testOnProgress(new TestCallback() {
@Override
public void onSuccess(String msg) {
Log.i(TAG, "onSuccess: " + msg);
tv.setText(msg);
}
@Override
public void onProgress(Byte[] data) {
if(data!=null){
Log.i(TAG, "onProgress: " + data.length);
tv.setText("onProgress: data size " + data.length);
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白如何正确声明 GetMethodID 中的 ByteArray 参数,但这是我的 logcat。
08-28 14:08:49.702 3015-3015/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: test.example.test, PID: 3015
java.lang.NoSuchMethodError: no non-static method "Ltest/example/test/MainActivity$1;.onProgress([B)V"
at test.example.test.MainActivity.testOnProgress(Native Method)
at test.example.test.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:6915)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
08-28 14:08:49.726 3015-3015/? I/Process: Sending signal. PID: 3015 SIG: 9
Run Code Online (Sandbox Code Playgroud)
但字符串没有问题。可以正常工作。这是字符串的一部分。
extern "C"
JNIEXPORT void JNICALL
Java_test_example_test_MainActivity_testOnSuccess(JNIEnv *env, jobject instance, jobject callback) {
//declare ref java class
jclass jClassTestCallBack = env->GetObjectClass(callback);
//declare java method id
jmethodID jMethodIdOnSuccess = env->GetMethodID(jClassTestCallBack,"onSuccess","(Ljava/lang/String;)V");
//check null
if(jMethodIdOnSuccess == 0){
return;
}
env->CallVoidMethod(callback,jMethodIdOnSuccess,env->NewStringUTF("Callback Success!!"));
}
Run Code Online (Sandbox Code Playgroud)
谢谢。
解决方案
我决定在 onProgress 中使用 byte[] 而不是 Byte[]。
[B意味着byte[],即原始类型的数组byte。
您的onProgress方法采用 class 的数组Byte,这不是一回事。
您可以:
TestCallback以便onProgress采用byte[].或者
GetMethodID呼叫,使其使用正确的签名([Ljava/lang/Byte;)V。这可能需要更改然后尝试调用该方法的任何代码。| 归档时间: |
|
| 查看次数: |
1887 次 |
| 最近记录: |