0

My application is behaving quit randomly. When I run it on API level 10. It works fine well. But when I run it on API level 14 it show following exceptions:

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 is as follows

/*
 * 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();
    }

}

Layout of this activity is:

<?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>

And manifest of my application is as follows:

    <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>

Is this some thing to do with this?

Community
  • 1
  • 1
Tariq
  • 2,489
  • 11
  • 36
  • 61

1 Answers1

1

mCamera.setPreviewCallback(previewCb); which is line 76 causing Exception due to mCamera is null, make sure you properly initialize your mCamera variable, since the method getCameraInstance() may return null, so you have to handle it accordingly like with try catch or check null before executing further.

The actual problem is Camera.open() is returning null,

Have a look at these posts

  1. Camera.open() returns null
  2. Camera.open() returns NULL Android development
Community
  • 1
  • 1
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153