我的应用程序在运行4.3的三星Galaxy S3上收到以下崩溃:
06-15 10:46:45.260 6649-6649/com.company.appname E/dalvikvm: Could not find class 'android.hardware.camera2.CameraManager', referenced from method com.company.appname.fragments.menu.MyFragment.flashLightOff
06-15 10:46:45.260 6649-6649/com.company.appname W/dalvikvm: VFY: unable to resolve check-cast 176 (Landroid/hardware/camera2/CameraManager;) in Lcom/company/appname/fragments/menu/MyFragment;
06-15 10:46:45.260 6649-6649/com.company.appname D/dalvikvm: VFY: replacing opcode 0x1f at 0x0010
06-15 10:46:45.265 6649-6649/com.company.appname W/dalvikvm: VFY: unable to resolve exception class 175 (Landroid/hardware/camera2/CameraAccessException;)
06-15 10:46:45.265 6649-6649/com.company.appname W/dalvikvm: VFY: unable to find exception handler at addr 0x1e
06-15 10:46:45.265 6649-6649/com.company.appname W/dalvikvm: VFY: rejected Lcom/company/appname/fragments/menu/MyFragment;.flashLightOff (Landroid/view/View;)V
06-15 10:46:45.265 6649-6649/com.company.appname W/dalvikvm: VFY: rejecting opcode 0x0d at 0x001e
06-15 10:46:45.265 6649-6649/com.company.appname W/dalvikvm: VFY: rejected Lcom/company/appname/fragments/menu/MyFragment;.flashLightOff (Landroid/view/View;)V
06-15 10:46:45.265 6649-6649/com.company.appname W/dalvikvm: Verifier rejected class Lcom/company/appname/fragments/menu/MyFragment;
06-15 10:46:45.265 6649-6649/com.company.appname D/AndroidRuntime: Shutting down VM
06-15 10:46:45.265 6649-6649/com.company.appname W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41d56700)
06-15 10:46:46.045 6649-6649/com.company.appname E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.VerifyError: com/company/appname/fragments/menu/MyFragment
at com.company.appname.adapters.NavigationCollectionPagerAdapter.<init>(NavigationCollectionPagerAdapter.java:16)
at com.company.appname.MainActivity.onCreate(MainActivity.java:84)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
我的gradle使用了我认为相关的这些设置:
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
...
}
...
dependencies {
compile 'com.android.support:appcompat-v7:23.0.1'
...
}
清单具有以下内容:
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.CAMERA" />
和罪魁祸首的方法是这样的:
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
...
public void flashLightOff(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
CameraManager manager = (CameraManager) BDApplication.getGlobalContext().getSystemService(Context.CAMERA_SERVICE);
manager.setTorchMode(manager.getCameraIdList()[0], false);
} catch (CameraAccessException cae) {
Logger.logError(cae.getMessage());
cae.printStackTrace();
}
}
else {
try {
if (getActivity().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FLASH)) {
cam.stopPreview();
cam.release();
cam = null;
}
} catch (Exception e) {
e.printStackTrace();
Logger.logError("Error turning off Flashlight", e);
}
}
}
当应用程序启动时发生此崩溃,特别是在创建片段时。永远不会调用此方法,从我可以阅读的所有内容(Build.VERSION.SDK_INT&gt; = Build.VERSION_CODES.M)后卫应该防止此类崩溃,但在某些设备上似乎没有。
我可以使用有限数量的测试设备,只有使用4.3的三星Galaxy S3才会发生这种崩溃。其他设备包括带有4.4的Note 2,带有5和6的变量设备,所有设备都按预期工作。
我对我的问题所处的位置感到茫然,有人能指出我正确的方向吗?
编辑:堆栈跟踪并没有真正反映我认为是罪魁祸首,堆栈跟踪是在创建包含“Camera2”clssses的片段时,但是此时从不调用它们。我相信崩溃是由于dalvik VFY无法解决错误。
答案 0 :(得分:0)
我已经通过将camera2类引用拉入一个新类,然后像上面那样保护它们来解决这个问题。
public void flashLightOff(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
FlashLightManager.flashlightOff();
}else
{
try {
if (getActivity().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FLASH)) {
cam.stopPreview();
cam.release();
cam = null;
}
} catch (Exception e) {
e.printStackTrace();
Logger.logError("Error turning off Flashlight", e);
}
}
}
经过一些测试后,我可能会将两者都移到“FlashLightManager”中。