无法启动接收器(包名称).Class On Camera.open()

时间:2017-08-02 08:10:46

标签: runtime-error flashlight receiver

创建小部件以打开和关闭Flash灯 关闭闪光灯时出错 我的小工具类代码

代码在onRecieve

@Override
    public void onReceive(Context context, Intent intent)
    {
        super.onReceive(context, intent);

        if(SYNC_CLICKED.equals(intent.getAction()))
        {
            AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(context);
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget__flash);
            ComponentName componentName=new ComponentName(context,Widget_Flash.class);

            if(Allowed_Flash) {
                if (!Flash_Light_On) {
                    getCamera();
                    Turn_On_Flash_Light();
                    views.setImageViewResource(R.id.Widget_On_Off_Button, R.drawable.button2);
                    Flash_Light_On = true;
                } else {
                    views.setImageViewResource(R.id.Widget_On_Off_Button, R.drawable.button1);
                        getCamera();
                        Turn_Off_Flash_Light();

                    }

                    Flash_Light_On = false;
                }

            }
            appWidgetManager.updateAppWidget(componentName, views);
        }
    }

获取相机

private void getCamera() {
        if (camera == null) {
            try {
                camera = Camera.open();
                params = camera.getParameters();
            } catch (RuntimeException e) {
            }
        }
    }

开启

 private void Turn_On_Flash_Light()
    {
        params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
    }

关闭

private void Turn_Off_Flash_Light()
{
    params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
    camera.setParameters(params);
    camera.startPreview();
    camera.release();
}

错误

  

E / AndroidRuntime:致命异常:主要                                                                        java.lang.RuntimeException:无法启动接收器   io.farooq.flash_light.Widget_Flash:java.lang.NullPointerException                                                                            在android.app.ActivityThread.handleReceiver(ActivityThread.java:2287)                                                                            在android.app.ActivityThread.access $ 1600(ActivityThread.java:140)                                                                            在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1313)                                                                            在android.os.Handler.dispatchMessage(Handler.java:99)                                                                            在android.os.Looper.loop(Looper.java:137)                                                                            在android.app.ActivityThread.main(ActivityThread.java:4921)                                                                            at java.lang.reflect.Method.invokeNative(Native Method)                                                                            在java.lang.reflect.Method.invoke(Method.java:511)                                                                            在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1038)                                                                            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)                                                                            at dalvik.system.NativeStart.main(Native Method)                                                                         引起:java.lang.NullPointerException                                                                            在   io.farooq.flash_light.Widget_Flash.Turn_Off_Flash_Light(Widget_Flash.java:143)                                                                            at io.farooq.flash_light.Widget_Flash.onReceive(Widget_Flash.java:74)                                                                            在android.app.ActivityThread.handleReceiver(ActivityThread.java:2280)                                                                            在android.app.ActivityThread.access $ 1600(ActivityThread.java:140)                                                                            在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1313)                                                                            在android.os.Handler.dispatchMessage(Handler.java:99)                                                                            在android.os.Looper.loop(Looper.java:137)                                                                            在android.app.ActivityThread.main(ActivityThread.java:4921)                                                                            at java.lang.reflect.Method.invokeNative(Native Method)                                                                            在java.lang.reflect.Method.invoke(Method.java:511)                                                                            在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1038)                                                                            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)                                                                            在dalvik.system.NativeStart.main(本地方法)

在三星J5 Prime(API 23)代码工作正常但是在试用(API 16)& (API 19)闪存正在打开但是在关闭时出错

0 个答案:

没有答案