0

我是安卓新手。我想知道为什么当我在我的模拟器中单击/按下时,它总是不幸的 cameraActivity 已停止这是我的 CameraTestActivity

public class CameraTestActivity extends Activity
{
private Camera mCamera;
private CameraPreview mPreview;
private Handler autoFocusHandler;

TextView scanText;
Button scanButton, btn_next;

ImageScanner scanner;

private boolean barcodeScanned = false;
private boolean previewing = true;

static {
    System.loadLibrary("iconv");
} 

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    autoFocusHandler = new Handler();
    mCamera = getCameraInstance();

    /* Instance barcode scanner */
    scanner = new ImageScanner();
    scanner.setConfig(0, Config.X_DENSITY, 3);
    scanner.setConfig(0, Config.Y_DENSITY, 3);

    mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
    FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
    preview.addView(mPreview);

    scanText = (TextView)findViewById(R.id.scanText);

    btn_next = (Button)findViewById(R.id.button1);
    btn_next.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent (CameraTestActivity.this,  
                             SecondActivity.class);
            startActivity(intent);
        }
    });

    scanButton = (Button)findViewById(R.id.ScanButton);

    scanButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (barcodeScanned) {
                    barcodeScanned = false;
                    scanText.setText("Scanning...");
                    mCamera.setPreviewCallback(previewCb);
                    mCamera.startPreview();
                    previewing = true;
                    mCamera.autoFocus(autoFocusCB);
                }
            }
        });
}

public void onPause() {
    super.onPause();
    releaseCamera();
}

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open();
    } catch (Exception e){
    }
    return c;
}

private void releaseCamera() {
    if (mCamera != null) {
        previewing = false;
        mCamera.setPreviewCallback(null);
        mCamera.release();
        mCamera = null;
    }
}

private Runnable doAutoFocus = new Runnable() {
        public void run() {
            if (previewing)
                mCamera.autoFocus(autoFocusCB);
        }
    };

PreviewCallback previewCb = new PreviewCallback() {
        public void onPreviewFrame(byte[] data, Camera camera) {
            Camera.Parameters parameters = camera.getParameters();
            Size size = parameters.getPreviewSize();

            Image barcode = new Image(size.width, size.height, "Y800");
            barcode.setData(data);

            int result = scanner.scanImage(barcode);

            if (result != 0) {
                previewing = false;
                mCamera.setPreviewCallback(null);
                mCamera.stopPreview();

                SymbolSet syms = scanner.getResults();
                for (Symbol sym : syms) {
                    scanText.setText("barcode result " + sym.getData());
                    barcodeScanned = true;
                }
            }
        }
    };

// Mimic continuous auto-focusing
AutoFocusCallback autoFocusCB = new AutoFocusCallback() {
        public void onAutoFocus(boolean success, Camera camera) {
            autoFocusHandler.postDelayed(doAutoFocus, 1000);
        }
    };
}

我的第二个活动没有任何功能。但是,每当我按回我的设备以返回 CameraTestActivity 时,它都会显示在 release() 之后调用的方法,这是我的 logcat 错误:

08-02 15:01:56.620: E/AndroidRuntime(7772): java.lang.RuntimeException: Method called 
after release()
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
android.hardware.Camera.setPreviewDisplay(Native Method)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
android.hardware.Camera.setPreviewDisplay(Camera.java:407)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at           
net.sourceforge.zbar.android.CameraTest.CameraPreview.surfaceCreated(CameraPreview.
java:69)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at    
android.view.SurfaceView.updateWindow(SurfaceView.java:533)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:226)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
android.view.View.dispatchWindowVisibilityChanged(View.java:5844)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:965)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
android.os.Handler.dispatchMessage(Handler.java:99)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
android.os.Looper.loop(Looper.java:137)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
android.app.ActivityThread.main(ActivityThread.java:4424)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
java.lang.reflect.Method.invokeNative(Native Method)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
java.lang.reflect.Method.invoke(Method.java:511)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
dalvik.system.NativeStart.main(Native Method)

有人可以帮我解决这个问题。

4

2 回答 2

0

查看您的日志:

08-02 15:01:56.620: E/AndroidRuntime(7772): java.lang.RuntimeException: Method called 
after release()
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
android.hardware.Camera.setPreviewDisplay(Native Method)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at 
android.hardware.Camera.setPreviewDisplay(Camera.java:407)
08-02 15:01:56.620: E/AndroidRuntime(7772):     at           
net.sourceforge.zbar.android.CameraTest.CameraPreview.surfaceCreated(CameraPreview.
java:69)

是被surfaceCreated调用后onPause吗?在你的onPause,你已经释放了你的相机。核实。

于 2013-08-02T07:32:22.343 回答
0

您应该实现 onResume() 方法,因为当您按下返回按钮后返回到上一个活动时会调用它。

于 2013-08-02T07:36:58.597 回答