我正在尝试添加缩放控件,但是当程序使用zoomControls变量执行任何操作时,我会得到空指针异常,即使我已经在我的XML和我的代码中定义了zoomcontrol。
我可以看到Log" assignement已完成"但不是"都支持"在第一个if语句中。我之前检查过两个缩放都是支持的。
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
ZoomControls zoomControls = (ZoomControls) findViewById(R.id.CAMERA_ZOOM_CONTROLS);
params = mCamera.getParameters();
Log.i("zoomControl", "assignement done");
if (params.isZoomSupported() && params.isSmoothZoomSupported()) {
//most phones
maxZoomLevel = params.getMaxZoom();
zoomControls.setIsZoomInEnabled(true);
Log.i("here", "both supported");
zoomControls.setIsZoomOutEnabled(true);
zoomControls.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel < maxZoomLevel) {
currentZoomLevel++;
mCamera.startSmoothZoom(currentZoomLevel);
}
}
});
zoomControls.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel > 0) {
currentZoomLevel--;
mCamera.startSmoothZoom(currentZoomLevel);
}
}
});
} else if (params.isZoomSupported() && !params.isSmoothZoomSupported()){
Log.i("here", "zoom supported and smooth not supported");
maxZoomLevel = params.getMaxZoom();
zoomControls.setIsZoomInEnabled(true);
zoomControls.setIsZoomOutEnabled(true);
zoomControls.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel < maxZoomLevel) {
currentZoomLevel++;
params.setZoom(currentZoomLevel);
mCamera.setParameters(params);
}
}
});
zoomControls.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel > 0) {
currentZoomLevel--;
params.setZoom(currentZoomLevel);
mCamera.setParameters(params);
}
}
});
}else{
//no zoom on phone
Log.i("here", "no zoom");
zoomControls.setVisibility(View.GONE);
}
// start preview with new settings
try {
Log.i("here", "try");
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
// intentionally left blank for a test
}
}//end of surfacechanged method
这是我在xml中的缩放控制声明
<ZoomControls
android:id="@+id/CAMERA_ZOOM_CONTROLS"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
这是日志:
11-20 17:26:00.667:我/这里(22140):开始,完成任务 11-20 17:26:00.667:D / AndroidRuntime(22140):关闭VM 11-20 17:26:00.667:W / dalvikvm(22140):threadid = 1:线程退出时未捕获异常(组= 0x416118b0) 11-20 17:26:00.677:E / AndroidRuntime(22140):致命异常:主要 11-20 17:26:00.677:E / AndroidRuntime(22140):java.lang.NullPointerException 11-20 17:26:00.677:E / AndroidRuntime(22140):at com.cellf.smartfieldtester.CameraPreview.surfaceChanged(CameraPreview.java:68) 11-20 17:26:00.677:E / AndroidRuntime(22140):在android.view.SurfaceView.updateWindow(SurfaceView.java:582) 11-20 17:26:00.677:E / AndroidRuntime(22140):在android.view.SurfaceView.access $ 000(SurfaceView.java:86) 11-20 17:26:00.677:E / AndroidRuntime(22140):在android.view.SurfaceView $ 3.onPreDraw(SurfaceView.java:175) 11-20 17:26:00.677:E / AndroidRuntime(22140):在android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:833) 11-20 17:26:00.677:E / AndroidRuntime(22140):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1868) 11-20 17:26:00.677:E / AndroidRuntime(22140):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1008) 11-20 17:26:00.677:E / AndroidRuntime(22140):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5509) 11-20 17:26:00.677:E / AndroidRuntime(22140):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749) 11-20 17:26:00.677:E / AndroidRuntime(22140):在android.view.Choreographer.doCallbacks(Choreographer.java:562) 11-20 17:26:00.677:E / AndroidRuntime(22140):在android.view.Choreographer.doFrame(Choreographer.java:532) 11-20 17:26:00.677:E / AndroidRuntime(22140):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735) 11-20 17:26:00.677:E / AndroidRuntime(22140):在android.os.Handler.handleCallback(Handler.java:730) 11-20 17:26:00.677:E / AndroidRuntime(22140):在android.os.Handler.dispatchMessage(Handler.java:92) 11-20 17:26:00.677:E / AndroidRuntime(22140):在android.os.Looper.loop(Looper.java:137) 11-20 17:26:00.677:E / AndroidRuntime(22140):在android.app.ActivityThread.main(ActivityThread.java:5214) 11-20 17:26:00.677:E / AndroidRuntime(22140):at java.lang.reflect.Method.invokeNative(Native Method) 11-20 17:26:00.677:E / AndroidRuntime(22140):at java.lang.reflect.Method.invoke(Method.java:525) 11-20 17:26:00.677:E / AndroidRuntime(22140):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:739) 11-20 17:26:00.677:E / AndroidRuntime(22140):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555) 11-20 17:26:00.677:E / AndroidRuntime(22140):at dalvik.system.NativeStart.main(Native Method)