title: Camera crashes after taking picture and hitting ok
body: <p>I am developing an app which sends an intent to take a picture. I am using a Galaxy S5 but this problem is confirmed on 3 different devices.</p>
  
  <p>First the camera started doing a loop where hitting OK just returned back to take another photo. I refactored  the code and fixed that problem but had others. Now, after bringing up the take photo screen, using the back button goes back successfully and hitting retry after taking the photo works fine. However hitting OK does one of three things: </p>
  
  <ul>
  <li>Works (~%20) </li>
  <li>throws an error <em>outside</em> my own error handler.
  "Unfortunately MyApp has stopped unexpectedly". Then it goes back to the calling activity, throwing an error from my handler because now it is trying to read from something which is now null (presumably because the app crashed). Breakpoints are now ignored and I have to launch debug again. I can go back to first page of app and go through process again, but debug doesn't respond. (~30%)</li>
  <li>Same as before except my error is thrown, I click the back button to get rid of it then the back button to go back a screen and then I get the "Unfortunately myApp has stopped". (~50%)</li>
  </ul>
  
  <p>I have <strong>not</strong> been able to find the error being thrown from the camera which causes the initial weirdness and then causes everything to crash. I have a breakpoint on onActivityResult which is never triggered if an error is thrown. Also my catch block on <code>startActivityForResult</code> never throws that error. The errors always come from NPE's in the onCreate as the activity tries to start up again after the photo is taken or the app throwing the "Unfortunately myApp has stopped". </p>
  
  <p>I have a ton more detail but I don't want to overload with info. Basically when I hit OK it either works ~20% of the time or all hell breaks loose the other 80% of the time. Can anyone point me in the right direction as to why the results are so seemingly random and unpredictable? Thanks!</p>
  
  <p>I have been following this tutorial:
  <a href="http://developer.android.com/training/camera/photobasics.html" rel="nofollow">http://developer.android.com/training/camera/photobasics.html</a></p>
  
  <p><strong>callingActivity</strong></p>
  
  <pre><code> private class SetCounterActivityMenu implements OnMenuItemClickListener {
    private FieldAppActivity a;
    private SetCounterActivityMenu(FieldAppActivity _a) {
      this.a = _a;
    }
  
    @Override
    public boolean onMenuItemClick(MenuItem view) {
      switch (view.getItemId()) {
      case 777771: {
        try{
          //nextActivity(CameraActivityNative.class);
          cameraAN = new CameraActivityNative(handler,SetCounterActivity.this);
          Intent intent = cameraAN.getPictureIntent();
          startActivityForResult(intent, cameraAN.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        }
        catch (Exception ex){
          handler.error("Error occured taking picture", ex);
        }
        break;
      }
      case 777772: {
        a.showMap(MapShowsWhat.survey);
        break;
      }
      }
      return false;
    }
  }
  </code></pre>
  
  <p><strong>CameraActivityNative.java</strong></p>
  
  <pre><code>public Intent getPictureIntent() {
      // create Intent to take a picture and return control to the calling application
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      // create a file to save the image
      try {
        fileUri = createMultimediaFile(MEDIA_TYPE_IMAGE, handler);
      }
      catch (Exception e){
        Utils.log("Camera Activity Native", "cannot create file", 'e');
      }
      // set the image file name
      intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
  
      // start the image Intent
      return intent;
    }
  private Uri createMultimediaFile(int type,    FieldApplicationManager.FAMEventHandler handler) throws   UnsupportedOperationException, IOException{
  // To be safe, you should check that the SDCard is mounted
  // using Environment.getExternalStorageState() before doing this.
  
    File mediaStorageDir = new   File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyDir");
  
    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
      if (! mediaStorageDir.mkdirs()){
        Utils.log("CameraActivityNative", "failed to create directory", 'd');
        return null;
      }
    }
  
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName;
  
    String fileSuffix;
    if (type == MEDIA_TYPE_IMAGE){
      imageFileName = "IMG_"+ timeStamp + ".jpg";
    }
    else if (type == MEDIA_TYPE_VIDEO) {
      imageFileName = "VID_"+ timeStamp + ".mp4";
    }
    else {
      throw new UnsupportedOperationException("Media type not supported");
    }
  
    File mediaFile = new File(mediaStorageDir, imageFileName);
  
    // Save a file: path for use with ACTION_VIEW intents
    // galleryAddPic("file:" + mediaFile.getAbsolutePath());
    return Uri.fromFile(mediaFile);
  }
  </code></pre>
  
  <p>Logcat error. I'm not sure this is actually relevant as this is the error that is thrown after the camera has already crashed.</p>
  
  <pre><code>02-27 17:56:27.609  17153-17153/android.fieldteam E/ERROR﹕ Message: Attempt to read from field 'java.lang.String android.api.CountSurvey.stationid' on a null object reference
      Last System Info: null
      Parameter: onGCFCreate
      Stack Trace: java.lang.NullPointerException: Attempt to read from field 'java.lang.String android.api.CountSurvey.stationid' on a null object reference
              at android.gui.SetCounterActivity.onGCFCreate(SetCounterActivity.java:33)
              at android.gui.FieldAppActivity.onCreate(FieldAppActivity.java:64)
              at android.app.Activity.performCreate(Activity.java:6221)
              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2728)
              at android.app.ActivityThread.access$900(ActivityThread.java:172)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:145)
              at android.app.ActivityThread.main(ActivityThread.java:5837)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
  </code></pre>