Android getSystemService(Context.CAMERA\u服务)崩溃
我有一个带有按钮的小部件,可以启用照相机服务 它工作得很好,但一段时间后(可能是在主屏幕应用程序退出时),它会失去状态(打开或关闭)Android getSystemService(Context.CAMERA\u服务)崩溃,android,android-camera,android-widget,Android,Android Camera,Android Widget,我有一个带有按钮的小部件,可以启用照相机服务 它工作得很好,但一段时间后(可能是在主屏幕应用程序退出时),它会失去状态(打开或关闭) 我有一个NullPointerException崩溃,我相信是来自Context.CAMERA\u服务。这真的很难重现,到目前为止,我还没有找到导致崩溃的顺序 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object andro
我有一个
NullPointerException
崩溃,我相信是来自Context.CAMERA\u服务
。这真的很难重现,到目前为止,我还没有找到导致崩溃的顺序
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.content.ContextWrapper.getSystemService(ContextWrapper.java:714)
at com.widget.tst.Widget.CallbackWidgetService.isCameraInUse(CallbackWidgetService.java:163)
at com.widget.tst.Widget.CallbackWidgetService.startCameraInUse(CallbackWidgetService.java:242)
//** button click-->
at com.widget.tst.Widget.CameraWidgetReceiver.updateWidgetButton(CameraWidgetReceiver.java:55)
at com.widget.tst.Widget.CameraWidgetReceiver.onReceive(CameraWidgetReceiver.java:32)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3632)
at android.app.ActivityThread.-wrap18(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1975)
at android.os.Handler.dispatchMessage(Handler.java:109)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7367)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)
Context.CAMERA\u服务
如何可以为空
private void isCameraInUse(){
if(context == null) return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
--> line 163 mCameraManager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
}
else{
Toast.makeText(context, "You need to run Android version "+
Build.VERSION_CODES.M+" or above",
Toast.LENGTH_SHORT).show();
return;
}
它看起来像是您当前类中的某个地方(
此
)它自己的上下文被置空,这就是为什么您获得NPE
但是既然变量中有100%的非空上下文(if(context==null)return;
),为什么不;你不能用它来代替这个自己的上下文吗
mCameraManager = (CameraManager)context.getSystemService(Context.CAMERA_SERVICE);
它看起来像是在当前类中的某个地方(
this
),它自己的上下文被置空,这就是为什么您会得到NPE
但是既然变量中有100%的非空上下文(if(context==null)return;
),为什么不;你不能用它来代替这个自己的上下文吗
mCameraManager = (CameraManager)context.getSystemService(Context.CAMERA_SERVICE);
尝试更改
mCameraManager=(CameraManager)getSystemService(Context.CAMERA\u服务)
tomCameraManager=(CameraManager)context.getSystemService(context.CAMERA\u服务)
您的上下文为空,您从错误的位置调用了isCameraInUse
。例如,从另一个线程。当包含此方法(并且它们是此方法的上下文)的活动或片段被激活时,您调用了isCameraInUse
destroyed@Vladyslav:我会尝试,但您不相信是Context.CAMERA\u服务为空。context.getSystemService将如何进行更改?@wizard。上下文不能为空?上下文。摄像头服务不为空。stacktrace指出,调用此方法的对象为null,而不是methodtry to changemCameraManager=(CameraManager)getSystemService(Context.CAMERA_SERVICE)的参数
tomCameraManager=(CameraManager)context.getSystemService(context.CAMERA\u服务)
您的上下文为空,您从错误的位置调用了isCameraInUse
。例如,从另一个线程。当包含此方法(并且它们是此方法的上下文)的活动或片段被激活时,您调用了isCameraInUse
destroyed@Vladyslav:我会尝试,但您不相信是Context.CAMERA\u服务为空。context.getSystemService将如何进行更改?@wizard。上下文不能为空?上下文。摄像头服务不为空。stacktrace表示调用此方法的对象为null,而不是该方法的参数