我有一个使用谷歌手机视觉(OCR),相机和手电筒的应用程序, 最近我几乎每天都在Android 6.0设备上收到崩溃报告。
这是报告:
java.lang.IllegalStateException:
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick (AppCompatViewInflater.java:293)
at android.view.View.performClick (View.java:5293)
at android.view.View$PerformClick.run (View.java:21660)
at android.os.Handler.handleCallback (Handler.java:815)
at android.os.Handler.dispatchMessage (Handler.java:104)
at android.os.Looper.loop (Looper.java:227)
at android.app.ActivityThread.main (ActivityThread.java:6100)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:859)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:749)
Caused by: java.lang.reflect.InvocationTargetException:
at java.lang.reflect.Method.invoke (Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick (AppCompatViewInflater.java:288)
Caused by: java.lang.NullPointerException:
at java.lang.reflect.Field.get (Native Method)
at myPacName.instantscanandrecharge.CamActivity.changeFlashStatus (CamActivity.java:412)
我不知道如何处理本报告中的原生方法
但是从我自己的代码中导致此错误的方法是:
public void changeFlashStatus(View v) {
Field[] declaredFields = CameraSource.class.getDeclaredFields();
for (Field field : declaredFields) {
if (field.getType() == Camera.class) {
field.setAccessible(true);
try {
camera = (Camera) field.get(camSource);
if (camera != null) {
params = camera.getParameters();
if(!isFlash){
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
flash_btn.setImageResource(R.drawable.ic_action_flash_on);
isFlash = true;
} else {
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
flash_btn.setImageResource(R.drawable.ic_action_flash_off);
isFlash = false;
}
camera.setParameters(params);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
}
为什么它不适用于Android 6.0?