title: zbar barcode scanner in a fragment
body: <p>I'm trying to get a barcode reader in a fragment but i'm receiving chrash. Does anybody has an idea about the problem? I'm new to fragments and don't know what I'm doing wrong. Please help..</p>
  
  <p>LogCat:</p>
  
  <pre><code>08-02 09:48:22.890: E/AndroidRuntime(11718): FATAL EXCEPTION: main
  08-02 09:48:22.890: E/AndroidRuntime(11718): java.lang.NullPointerException
  08-02 09:48:22.890: E/AndroidRuntime(11718):    at com.learn2crack.tab.Android$2.onPreviewFrame(Android.java:106)
  08-02 09:48:22.890: E/AndroidRuntime(11718):    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:864)
  08-02 09:48:22.890: E/AndroidRuntime(11718):    at android.os.Handler.dispatchMessage(Handler.java:99)
  08-02 09:48:22.890: E/AndroidRuntime(11718):    at android.os.Looper.loop(Looper.java:176)
  08-02 09:48:22.890: E/AndroidRuntime(11718):    at android.app.ActivityThread.main(ActivityThread.java:5365)
  08-02 09:48:22.890: E/AndroidRuntime(11718):    at java.lang.reflect.Method.invokeNative(Native Method)
  08-02 09:48:22.890: E/AndroidRuntime(11718):    at java.lang.reflect.Method.invoke(Method.java:511)
  08-02 09:48:22.890: E/AndroidRuntime(11718):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
  08-02 09:48:22.890: E/AndroidRuntime(11718):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
  08-02 09:48:22.890: E/AndroidRuntime(11718):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>and the code:</p>
  
  <pre><code>public class Android extends Fragment {
  
   public Android(){}
  
   private Camera mCamera;
   private CameraPreview mPreview;
   private Handler autoFocusHandler;
   private static final String TAG_PID = "pid";
  
   TextView scanText;
   ImageScanner scanner;
   private boolean barcodeScanned = false;
   private boolean previewing = true;
   FrameLayout preview;
  
   static {
       System.loadLibrary("iconv");
   }
  
      public void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
  
      }
  
      @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
  
          View android = inflater.inflate(R.layout.android_frag, container, false);
          autoFocusHandler = new Handler();
          mCamera = getCameraInstance();
          mPreview = new CameraPreview(this.getActivity(), mCamera, previewCb, autoFocusCB);
          preview = (FrameLayout) android.findViewById(R.id.cameraPreview);
          preview.addView(mPreview);
  
          scanText = (TextView) android.findViewById(R.id.scanText);       
  
          return android;
  
      }
  
  
      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);
              }
          };
  
  }
  </code></pre>