我正在尝试绘制一些图形的应用程序,因为我使用了Surface View ..当我点击“on”按钮时,我的应用程序必须打开相机以获取一些参数来绘制一些图表和类似的当我点击“关闭”按钮我需要停止相机预览以及绘图图..这一切都适用于我的一次尝试,但当我试图重复它然后我的应用程序崩溃。
中显示错误public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
System.out.println("Deleted");
try { mCamera.setPreviewDisplay(sh);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
这是我的代码......
public void startCamPreview() {
SurfaceView sv = (SurfaceView) findViewById(R.id.surfaceView);
final SurfaceHolder sh = sv.getHolder();
mCamera = Camera.open();
timer.start();
try {
mCamera.setPreviewDisplay(sh);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sh.addCallback(new Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
System.out.println("Deleted");
try { mCamera.setPreviewDisplay(sh);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
System.out.println("Created");
mCamera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters params = camera.getParameters();
int width = params.getPreviewSize().width;
int height = params.getPreviewSize().height;
byte[] mYUVData = new byte[data.length];
int[] mRGBData = new int[width * height];
System.arraycopy(data, 0, mYUVData, 0, data.length);
decodeYUV420SP(mRGBData, mYUVData, width, height);
int[] mRedHistogram = new int[256];
calculateIntensityHistogram(mRGBData, mRedHistogram,
width, height, 0);
double imageRedMean = 0;
double redHistogramSum = 0;
for (int bin = 0; bin < 256; bin++) {
imageRedMean += mRedHistogram[bin] * bin;
redHistogramSum += mRedHistogram[bin];
} // bin
imageRedMean /= redHistogramSum;
double[] mBinSquared;
mBinSquared = new double[256];
for (int bin = 0; bin < 256; bin++) {
mBinSquared[bin] = ((double) bin) * bin;
}
double imageRed2ndMoment = 0;
for (int bin = 0; bin < 256; bin++) {
imageRed2ndMoment += mRedHistogram[bin]
* mBinSquared[bin];
} // bin
imageRed2ndMoment /= redHistogramSum;
double imageRedStdDev = Math.sqrt((imageRed2ndMoment
- imageRedMean * imageRedMean));
int val = (new Double(imageRedStdDev * 100000)
.intValue());
for (int l = 0; l < 1; l++) {
}
int yVal=250;
if (imageRedStdDev < 15 && val != 0)
{
mp.release();
show=true;
g.addPoint(yVal);
g.addPoint(yVal);
float flt=(float)Math.random();
g.addPoint(yVal+flt*25);
g.addPoint(yVal);
g.addPoint(yVal);
flt=(float)Math.random();
g.addPoint(yVal-flt*20);
g.addPoint(yVal);
g.addPoint(yVal);
flt=(float)Math.random();
g.addPoint(yVal-flt*30);
g.addPoint(yVal);
g.addPoint(yVal);
g.addPoint(yVal);
g.addPoint(yVal);
flt=(float)Math.random();
g.addPoint(yVal+flt*35);
mp=MediaPlayer.create(getApplicationContext(), R.raw.heartbeat);
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
//mp.setLooping(true);
Random r=new Random();
no=r.nextInt(84-60)+60;
bpmText_View.setText(String.valueOf(no));
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
show=false;
if(val==0){
count++;
g.addPoint(yVal);
g.addPoint(yVal);
g.addPoint(yVal);
g.addPoint(yVal);
mp.release();
mp=MediaPlayer.create(getApplicationContext(), R.raw.beep);
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
bpmText_View.setText(String.valueOf(0));
}
else{
mp.release();
count++;
g.addPoint(yVal);
g.addPoint(yVal);
g.addPoint(yVal);
g.addPoint(yVal);
mp=MediaPlayer.create(getApplicationContext(), R.raw.beep);
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
bpmText_View.setText(String.valueOf(0));
}
}
}
});
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
System.out.println("Changed");
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(320, 240);
parameters.setPreviewFrameRate(15);
parameters.setSceneMode(Camera.Parameters.SCENE_MODE_NIGHT);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
mCamera.setParameters(parameters);
mCamera.startPreview();
g.invalidate();
}
});
// fl.addView(b);
}
这是我的日志猫显示
02-13 15:06:09.023:E / AndroidRuntime(13275):致命异常:主要 02-13 15:06:09.023:E / AndroidRuntime(13275):java.lang.RuntimeException:release()之后调用的方法 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.hardware.Camera.stopPreview(Native Method) 02-13 15:06:09.023:E / AndroidRuntime(13275):at com.yakshna.h2h.Graph $ 3.surfaceDestroyed(Graph.java:114) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.view.SurfaceView.reportSurfaceDestroyed(SurfaceView.java:587) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.view.SurfaceView.updateWindow(SurfaceView.java:481) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:213) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.view.View.dispatchDetachedFromWindow(View.java:6222) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.view.ViewRoot.dispatchDetachedFromWindow(ViewRoot.java:1751) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.view.ViewRoot.doDie(ViewRoot.java:2832) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.view.ViewRoot.die(ViewRoot.java:2802) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:218) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.view.Window $ LocalWindowManager.removeViewImmediate(Window.java:436) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2705) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.app.ActivityThread.access $ 2100(ActivityThread.java:117) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:968) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.os.Handler.dispatchMessage(Handler.java:99) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.os.Looper.loop(Looper.java:123) 02-13 15:06:09.023:E / AndroidRuntime(13275):在android.app.ActivityThread.main(ActivityThread.java:3687) 02-13 15:06:09.023:E / AndroidRuntime(13275):at java.lang.reflect.Method.invokeNative(Native Method) 02-13 15:06:09.023:E / AndroidRuntime(13275):at java.lang.reflect.Method.invoke(Method.java:507) 02-13 15:06:09.023:E / AndroidRuntime(13275):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:842) 02-13 15:06:09.023:E / AndroidRuntime(13275):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 02-13 15:06:09.023:E / AndroidRuntime(13275):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
你mCamera.open()在与.startPreview()不同的线程中:
参考:http://developer.android.com/reference/android/hardware/Camera.html
“这个类不是线程安全的,可以在一个事件线程中使用。大多数长时间运行的操作(预览,焦点,照片捕获等)都是异步发生的,并在必要时调用回调。回调将在上面调用调用事件线程open(int)。不能一次从多个线程调用此类的方法。“
答案 1 :(得分:0)
我同意您需要发布实际错误,但在第一次检查时,我认为它是NullPointerException
。
我的第一个猜测是你有竞争条件。 mCamera
在surfaceDestroyed
和startCamPreview
方法之外定义,但两者都引用(包括Callback
匿名类的闭包)。