我已经构建了自定义相机应用程序。面对屏幕方向的问题,我不希望在方向更改时重新创建活动。请用完整的解决方案帮助我。
在预览类的surfaceChanged方法中获取NULL指针异常。
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (mCamera != null) {
Camera.Parameters parameters = mCamera.getParameters();
Log.d("SurfaceChanged", "getting parameters" + parameters);
在下面的行中获取NULL指针异常:
Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Log.d("SurfaceChanged", "Getting the system services");
if(display.getRotation() == Surface.ROTATION_0){
Log.d("SurfaceChanged", "Inside the if condition 1");
parameters.setPreviewSize(height,width);
Log.d("SurfaceChanged", "setting preview size");
mCamera.setDisplayOrientation(90);
Log.d("SurfaceChanged", "setting display orientation as 90");
}
if(display.getRotation() == Surface.ROTATION_90){
Log.d("SurfaceChanged", "Inside the if condition 2");
parameters.setPreviewSize(width,height);
Log.d("SurfaceChanged", "setting the preview size 2 ");
}
if(display.getRotation() == Surface.ROTATION_180){
Log.d("SurfaceChanged", "Inside the if condition 3");
parameters.setPreviewSize(height,width);
Log.d("SurfaceChanged", "setting the preview size 3");
}
if(display.getRotation() == Surface.ROTATION_270){
Log.d("SurfaceChanged", "Inside the if condition 4");
parameters.setPreviewSize(width,height);
Log.d("SurfaceChanged", "setting preview size 4");
mCamera.setDisplayOrientation(180);
Log.d("SurfaceChanged", "setting display orientation as 180");
}
mCamera.setParameters(parameters);
Log.d("SurfaceChanged", "setting the parameters");
try {
mCamera.setPreviewDisplay(mHolder);
Log.d("SurfaceChanged", "setting preview display to mHolder");
mCamera.startPreview();
Log.d("SurfaceChanged", "preview as started");
} catch (IOException e) {
e.printStackTrace();
}
}
}
非常感谢任何帮助。
12-23 16:07:34.962 8845-8845 / com.clarity.camera E / AndroidRuntime:FATAL EXCEPTION:main 处理:com.clarity.camera,PID:8845 java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.Object android.content.Context.getSystemService(java.lang.String)' 在com.clarity.camera.CameraPreview.surfaceChanged(CameraPreview.java:230) 在android.view.SurfaceView.updateWindow(SurfaceView.java:583) 在android.view.SurfaceView.access $ 000(SurfaceView.java:86) 在android.view.SurfaceView $ 3.onPreDraw(SurfaceView.java:175) 在android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1897) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5725) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761) 在android.view.Choreographer.doCallbacks(Choreographer.java:574) 在android.view.Choreographer.doFrame(Choreographer.java:544) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747) 在android.os.Handler.handleCallback(Handler.java:733) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5086) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
答案 0 :(得分:0)
如果您不希望销毁Activity
,可以将其放入manifest file
的活动代码中:
android:configChanges="orientation|screenSize|keyboardHidden"
这将告诉Android您将自己处理orientationChanges
。如果您使用Android API 13或更高版本进行开发,screenSize
非常重要。如果您正在开发Android API 12或更低版本,则可以退出screenSize
!
这意味着onDestroy
将不再被调用。而是调用onConfigurationChanged
,并且当设备的旋转发生时,您可以处理此方法中您想要做的事情。
见这里:http://developer.android.com/guide/topics/resources/runtime-changes.html
为了帮助您处理NullPointerException,您需要发布LogCat输出并告诉我们确切地抛出NPE的哪一行!