我正尝试通过根据以下代码设置字节数组参数来通过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);
}
在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);
}
}
});
}
}
我不明白如何在GetMethodID中正确声明ByteArray参数,但这是我的日志。
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
但是字符串没有问题。它可以正常工作。 这是字符串的一部分。
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!!"));
}
谢谢。
EDITE :这是我的TestCallBack类,以获取更多信息。
public interface TestCallback {
void onSuccess(String msg);
void onProgress(Byte[] data);
}
答案 0 :(得分:1)
[B
的意思是byte[]
,即原始类型byte
的数组。
您的onProgress
方法采用Byte
类的数组,这不是一回事。
您可以:
TestCallback
的声明,以使onProgress
取byte[]
。或
GetMethodID
通话,使其使用正确的签名([Ljava/lang/Byte;)V
。这可能需要更改然后尝试调用该方法的任何代码。