0

我的应用程序行为随机退出。当我在 API 级别 10 上运行它时。它运行良好。但是当我在 API 级别 14 上运行它时,它显示以下异常:

12-26 12:19:18.021: W/dalvikvm(1594): threadid=1: thread exiting with uncaught exception (group=0xb5f18288)
12-26 12:19:18.051: E/AndroidRuntime(1594): FATAL EXCEPTION: main
12-26 12:19:18.051: E/AndroidRuntime(1594): java.lang.RuntimeException: Unable to start activity ComponentInfo{tariq.buynow3/tariq.buynow3.CameraTestActivity}: java.lang.NullPointerException
12-26 12:19:18.051: E/AndroidRuntime(1594):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-26 12:19:18.051: E/AndroidRuntime(1594):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-26 12:19:18.051: E/AndroidRuntime(1594):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-26 12:19:18.051: E/AndroidRuntime(1594):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-26 12:19:18.051: E/AndroidRuntime(1594):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 12:19:18.051: E/AndroidRuntime(1594):     at android.os.Looper.loop(Looper.java:137)
12-26 12:19:18.051: E/AndroidRuntime(1594):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-26 12:19:18.051: E/AndroidRuntime(1594):     at java.lang.reflect.Method.invokeNative(Native Method)
12-26 12:19:18.051: E/AndroidRuntime(1594):     at java.lang.reflect.Method.invoke(Method.java:511)
12-26 12:19:18.051: E/AndroidRuntime(1594):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-26 12:19:18.051: E/AndroidRuntime(1594):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-26 12:19:18.051: E/AndroidRuntime(1594):     at dalvik.system.NativeStart.main(Native Method)
12-26 12:19:18.051: E/AndroidRuntime(1594): Caused by: java.lang.NullPointerException
12-26 12:19:18.051: E/AndroidRuntime(1594):     at tariq.buynow3.CameraTestActivity.onCreate(CameraTestActivity.java:76)
12-26 12:19:18.051: E/AndroidRuntime(1594):     at android.app.Activity.performCreate(Activity.java:5008)
12-26 12:19:18.051: E/AndroidRuntime(1594):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-26 12:19:18.051: E/AndroidRuntime(1594):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-26 12:19:18.051: E/AndroidRuntime(1594):     ... 11 more

CameraTestActivity如下

/*
 * Basic no frills app which integrates the ZBar barcode scanner with
 * the camera.
 * 
 * Created by lisah0 on 2012-02-24
 */
package tariq.buynow3;

import net.sourceforge.zbar.Config;
import net.sourceforge.zbar.Image;
import net.sourceforge.zbar.ImageScanner;
import net.sourceforge.zbar.Symbol;
import net.sourceforge.zbar.SymbolSet;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
/* Import ZBar Class files */

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

    TextView scanText;
    Button scanButton;

    ImageScanner scanner;

    @SuppressWarnings("unused")
    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);
        FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
        mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
        preview.addView(mPreview);
        scanText = (TextView)findViewById(R.id.scanText);
        Intent in = new Intent();
        setResult(-1,in);

        /*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();
                    String resultStr="";
                    for (Symbol sym : syms) {
                        resultStr += sym.getData();
                        barcodeScanned = true;
                    }
                    scanText.setText("Done");
                    Intent intent=new Intent();
                    intent.putExtra("Text",resultStr);
                    setResult(1, intent);
                    finish();
                }
            }
        };

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


    public void CancelScan(View view){
        barcodeScanned = false;
        /*Intent in = new Intent();
        setResult(-1,in);*/
        finish();
    }

}

本次活动布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <FrameLayout
    android:id="@+id/cameraPreview"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
  />

  <TextView
      android:id="@+id/scanText"
      android:layout_width="106dp"
      android:layout_height="wrap_content"
      android:text="@string/scanning" />

  <Button
      android:id="@+id/button1"
      style="?android:attr/buttonStyleSmall"
      android:layout_width="144dp"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:text="@string/Cancel"
      android:onClick="CancelScan" />

</LinearLayout>

我的申请清单如下:

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="tariq.buynow3.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="tariq.buynow3.CameraTestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity
            android:name="tariq.buynow3.BuyingActivity"
            android:label="@string/title_activity_buying" >
        </activity>
    </application>

</manifest>

这与有关吗?

4

1 回答 1

1

mCamera.setPreviewCallback(previewCb);这是第 76 行,由于mCamerais导致异常null,请确保正确初始化mCamera变量,因为该方法getCameraInstance()可能会返回,因此您必须在进一步执行之前像使用或检查null一样相应地处理它。try catchnull

实际的问题Camera.open()是返回 null,

看看这些帖子

  1. Camera.open() 返回 null
  2. Camera.open() 返回 NULL Android 开发
于 2012-12-26T08:12:04.033 回答