android api 21+ camera2 api

1.2k views Asked by At

i try to run this code, but it say that there is some thing not found in framworke.jar

 super.onCreate(savedInstanceState);
    CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

    String id = null;
    try {
        id = manager.getCameraIdList()[0];
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
    try {
        manager.setTorchMode(id,true);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }

but i got this error,

02-15 01:03:33.794 32378-32378/com.phenix.testcam E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: com.phenix.testcam, PID: 32378
                                                                java.lang.NoSuchMethodError: No virtual method setTorchMode(Ljava/lang/String;Z)V in class Landroid/hardware/camera2/CameraManager; or its super classes (declaration of 'android.hardware.camera2.CameraManager' appears in /system/framework/framework.jar)
                                                                    at com.phenix.testcam.MainActivity.onCreate(MainActivity.java:29)
                                                                    at android.app.Activity.performCreate(Activity.java:5990)
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:156)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                    at android.os.Looper.loop(Looper.java:211)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5373)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

plz i need some explanations

1

There are 1 answers

1
ianhanniballake On BEST ANSWER

setTorchMode() was added in API 23 - API 21-22 devices will not have that method.