Reputation: 23
I'm trying to call a java method via JNI by setting a byte array param according to the code below.
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);
}
In 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);
}
}
});
}
}
I don't understand how to declare the ByteArray parameter in GetMethodID correctly but this is my 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
But no problems with the string. It can work normally. This is part of the string.
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!!"));
}
Thank you.
Solution
I decided to takes a byte[] in onProgress instead of Byte[].
Upvotes: 2
Views: 2038
Reputation: 58467
[B
means byte[]
, i.e. an array of the primitive type byte
.
Your onProgress
method takes an array of the class Byte
, which is not the same thing.
You can either:
TestCallback
so that onProgress
takes a byte[]
.or
GetMethodID
call so that it uses the correct signature ([Ljava/lang/Byte;)V
. This may require changes to any code that then tries to call that method.Upvotes: 5