JNI编程报错java.lang.NoSuchMethodError: no non-static method "xxx"

08-16 20:55:41.769 E/AndroidRuntime(23151): java.lang.NoSuchMethodError: no non-static method "Lcom/example/jniffmpegstaticplay/JNIffPlay
er;.onPrepareFromJNI()"
08-16 20:55:41.769 E/AndroidRuntime(23151):     at com.example.jniffmpegstaticplay.JNIffPlayer.native_prepare(Native Method)
08-16 20:55:41.769 E/AndroidRuntime(23151):     at com.example.jniffmpegstaticplay.JNIffPlayer.prepare(JNIffPlayer.java:46)
08-16 20:55:41.769 E/AndroidRuntime(23151):     at com.example.jniffmpegstaticplay.MainActivity$1.onClick(MainActivity.java:54)
08-16 20:55:41.769 E/AndroidRuntime(23151):     at android.view.View.performClick(View.java:7192)
08-16 20:55:41.769 E/AndroidRuntime(23151):     at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:111

现象:直接闪退,日志不多
用adb命令adb shell logcat -v time > D:\logcat.txt抓取到上面日志

原因onPrepareFromJNI方法找不到,检查路径发现没问题,怀疑是JNI反射调用Java方法签名有问题。

  1. 找到出问题的class文件


    2021-08-16_214938.png
  2. cmd窗口查看类的方法签名:
$ cd 文件路径
$ javap -s -p JNIffPlayer  

得到结果:

 public void prepare();
    descriptor: ()V

  public void setDataSource(java.lang.String);
    descriptor: (Ljava/lang/String;)V

  public void onPrepareFromJNI();
    descriptor: ()V  
    # 方法签名是:()V 

  public void onProgressFromJNI(int);
    descriptor: (I)V

  public void onErrorFromJNI(int);
    descriptor: (I)V

  public void start();
    descriptor: ()V

  public void setOnPrepareListener(com.example.jniffmpegstaticplay.JNIffPlayer$OnPrepareListener);
    descriptor: (Lcom/example/jniffmpegstaticplay/JNIffPlayer$OnPrepareListener;)V

解决
修改反射方法的签名:

2021-08-16_215823.png

你可能感兴趣的:(JNI编程报错java.lang.NoSuchMethodError: no non-static method "xxx")