1

我创建了一个自定义相机应用程序,它在几乎所有设备上都能正常工作,但它似乎在开发人员门户中列为其他设备的设备上崩溃。这些用户都给我不好的评价我想解决这个问题,但我很难弄清楚,因为它在我拥有的所有设备上都能完美运行。

camera.setPreviewDisplay(holder);是导致错误的原因,它在应用程序启动时发生。`

public class CamPreview extends SurfaceView implements SurfaceHolder.Callback
{
SurfaceHolder holder;
Camera camera;
private List<Camera.Size> sizes;

public CamPreview(Context context, Camera camera)
{
    super(context);
    this.camera = camera;
    holder = this.getHolder();

           holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    holder.addCallback(this);

}

private Camera.Size getBestPreviewSize(int width, int height)
{
    Camera.Size result=null;
    Camera.Parameters p = camera.getParameters();
    for (Camera.Size size : p.getSupportedPreviewSizes()) {
        if (size.width<=width && size.height<=height) {
            if (result==null) {
                result=size;
            } else {
                int resultArea=result.width*result.height;
                int newArea=size.width*size.height;

                if (newArea>resultArea) {
                    result=size;
                }
            }
        }
    }
    return result;

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
    if (holder.getSurface() == null) return;

    Camera.Parameters parameters = camera.getParameters();

    sizes = parameters.getSupportedPreviewSizes();
    Camera.Size optimalSize = getBestPreviewSize(width, height);
    try{
    parameters.setPreviewSize(optimalSize.width,optimalSize.height);
        camera.setParameters(parameters);

    }
    catch (NullPointerException a)
    {

    }
    camera.startPreview();


}


@Override
public void surfaceCreated(SurfaceHolder holder)
{

    try
    {
        camera.setPreviewDisplay(holder);
    }
    catch (IOException e)
    {
        camera.release();
        camera=null;
        Log.d("FrontCam", "Error creating surface: " + e.getMessage());
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
    if (camera != null) {
        camera.stopPreview();
        camera.setPreviewCallback(null);
        camera.release();
    }


}
}

日志猫:

    java.lang.NullPointerException
    at com.me.frontcam.CamPreview.surfaceCreated(CamPreview.java:136)
    at android.view.SurfaceView.updateWindow(SurfaceView.java:552)
    at android.view.SurfaceView.dispatchDraw(SurfaceView.java:350)
    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    at android.view.View.draw(View.java:6883)
    at android.widget.FrameLayout.draw(FrameLayout.java:357)
    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    at android.view.View.draw(View.java:6883)
    at android.widget.FrameLayout.draw(FrameLayout.java:357)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1921)
    at android.view.ViewRoot.draw(ViewRoot.java:1528)
    at android.view.ViewRoot.performTraversals(ViewRoot.java:1264)
    at android.view.ViewRoot.handleMessage(ViewRoot.java:1866)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    at dalvik.system.NativeStart.main(Native Method)

我如何打开相机

void initCamera()
{
    //check for front camera
    if(Camera.getNumberOfCameras()>1)
    {

     camera= Camera.open(1);
        try
        {
                camera.cancelAutoFocus();
        }
        catch (RuntimeException a)
        {

        }


        try{

            camera.setDisplayOrientation(90);
        }
        catch (NullPointerException a)
        {
            camera.setDisplayOrientation(90);
        }

    }

    else
    {
        new AlertDialog.Builder(this)
                .setTitle("Sorry but you cant use this app")
                .setMessage("You do not have a front camera")
                .setPositiveButton("Quit", new DialogInterface.OnClickListener() {

                    //Quits application
                    public void onClick(DialogInterface dialog, int which) {
                       finish();
                    }
                })

                .show();
    }



}

然后我调用调用 CamPreview 构造函数的 initUI 方法

void initUI()
{
    white= new ImageView(this);
    imgShoot = (ImageButton) findViewById(R.id.imgShoot);
    imgShoot.setOnClickListener(imgShootOnClick);

     frmPreview = (FrameLayout) findViewById(R.id.frmPreview);
    preview = new CamPreview(this, camera);

    frmPreview.addView(white);
    white.setImageResource(R.drawable.aperture_opening);
    frmPreview.addView(preview);
    frmPreview.bringChildToFront(preview);  

}

我看了这两个,他们没有解决我的问题 Android - cam.setPreviewDisplay(holder) 运行到 IOError

.setPreviewDisplay(holder) 抛出空指针异常

任何帮助都非常感谢!

4

3 回答 3

3

请检查您是否在清单中添加了所需的权限,这对我有用

于 2014-06-30T09:48:05.017 回答
2

如果您在上面显示NullPointerExceptionsurfaceCreated()方法中使用 a 崩溃,那么您的问题应该很明显:cameranull.

由于您要传入camera的构造函数CamPreview,因此您需要调查如何调用此构造函数。

于 2013-07-22T19:52:15.367 回答
0

camera如果您的方法中的对象或holder对象为空,我会建议您先检查一下surfaceCreathed。您还应该检查您是否已经打开了相机:camera.open()

于 2013-07-22T19:52:31.837 回答