2013-04-12 71 views
0

在我單擊一個按鈕的主要活動中,單擊以下活動啓動以使用默認相機拍攝圖像在android中應用,然後錄製語音。無法啓動活動ComponentInfo {com.naviiid.soundshot/com.naviiid.soundshot.MicroFilm}:java.lang.NullPointerException

然而,每當我點擊我的主要活動按鈕我得到這個錯誤:

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.naviiid.soundshot/com.naviiid.soundshot.MicroFilm}: 
java.lang.NullPointerException 

有一個點的相機應用程序啓動,但我的應用程序已經停止之前! 下面的代碼(原諒我的代碼有點長):

package com.naviiid.soundshot; 

import java.io.File 
import java.util.Calendar; 
import android.R.drawable; 
import android.app.Activity; 
import android.content.Intent; 
import android.graphics.drawable.Drawable; 
import android.media.MediaRecorder; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ImageView; 
import android.widget.Toast; 

public class MicroFilm extends Activity { 

    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; 
    private Uri imageUri; 
    public static final int MEDIA_TYPE_IMAGE = 1; 
    public static final int MEDIA_TYPE_VIDEO = 2; 
    private static File mediaStorageDir; 
    private ImageView image; 
    private ImageView record; 
    private ImageView save; 
    private ImageView capture; 
    private boolean visibleButtons = true; 
    private MediaRecorder soundRecorder; 
    private String mediaFilePath; 
    private static Calendar cal; 
    boolean recording = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_micro_film); 

     String mediaState = Environment.getExternalStorageState(); 
     if (!mediaState.equals(Environment.MEDIA_MOUNTED)){ 
      Toast.makeText(MicroFilm.this, "Media unmounted! \n Mount media firts..", Toast.LENGTH_LONG).show(); 
      Log.d("StorageState", "Media Unmounted"); 
      finish(); 
     } 

     mediaStorageDir = new File(Environment 
       .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) 
       .getAbsolutePath() 
       + "/MicroFilm"); 

     takePicture(); // this method will capture image from camera intent and 
         // returns to main activity if user cancels 

     // View things 
     image = (ImageView) findViewById(R.id.imageViewPicture); 
     record = (ImageView) findViewById(R.id.imageViewMic); 
     save = (ImageView) findViewById(R.id.imageViewSave); 
     capture = (ImageView) findViewById(R.id.imageViewCapture); 

     setBackgroundImage(); // shows taken picture 
     captureSound(); // prepares for recording, recording starts with imageButton click 

    } 

    private void captureSound() { 
     // prepare things to record 
     soundRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     soundRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); 
     soundRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
     File soundFile = getNewSoundFile(); 
     soundRecorder.setOutputFile(soundFile.getAbsolutePath()); 
     try{ 
      soundRecorder.prepare(); 
     }catch (Exception e) { 
      Log.d("SoundRecorder", "unable to prepare"); 
      Toast.makeText(MicroFilm.this, "Mic in use", Toast.LENGTH_SHORT).show(); 
      return; 
     } 

     //listeners 
     soundRecorderListeners(); 

    } 

    private void soundRecorderListeners() { 

     record.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       if (recording){ 
        soundRecorder.stop(); 
        soundRecorder.release(); 
        recording = false; 
        record.setBackgroundResource(drawable.ic_btn_speak_now); 
       }else{ 
        soundRecorder.start(); 
        recording = true; 
        record.setBackgroundResource(drawable.presence_audio_busy); 
       } 
      } 
     }); 
    } 

    private File getNewSoundFile() { 
     // Create a media file name 
     String timeStamp; 
     cal = Calendar.getInstance(); 
     timeStamp = "" + cal.get(Calendar.YEAR) + cal.get(Calendar.MONTH) + 1 
       + cal.get(Calendar.DAY_OF_MONTH) + "_" 
       + cal.get(Calendar.HOUR_OF_DAY) + cal.get(Calendar.MINUTE) 
       + cal.get(Calendar.SECOND); 
     mediaFilePath = mediaStorageDir.getAbsolutePath() + "/AUD_" + timeStamp 
       + ".amr"; 
     return new File(mediaFilePath); 
    } 

    private void setBackgroundImage() { 
     // shows taken picture in background 
     image.setImageDrawable(Drawable.createFromPath(imageUri.getPath())); 
     image.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       if (visibleButtons) { 
        record.setVisibility(View.INVISIBLE); 
        save.setVisibility(View.INVISIBLE); 
        capture.setVisibility(View.INVISIBLE); 
        visibleButtons = false; 
       } else { 
        record.setVisibility(View.VISIBLE); 
        save.setVisibility(View.VISIBLE); 
        capture.setVisibility(View.VISIBLE); 
        visibleButtons = true; 
       } 
      } 
     }); 
    } 

    private void takePicture() { 
     Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     imageUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to 
     // save the image 
     camera.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); // set the image file 
     // name 

     // start the image captugetOutputMediaFileUrire Intent 
     startActivityForResult(camera, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 

    } 

    /** Create a file Uri for saving an image or video */ 
    private static Uri getOutputMediaFileUri(int type) { 
     return Uri.fromFile(getOutputMediaFile(type)); 
    } 

    /** Create a File for saving an image or video */ 
    private static File getOutputMediaFile(int type) { 
     // To be safe, you should check that the SDCard is mounted 
     // using Environment.getExternalStorageState() before doing this. 


     // Environment 
     // .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
     // "MyCameraApp"); 
     // This location works best if you want the created images to be shared 
     // between applications and persist after your app has been uninstalled. 

     // Create the storage directory if it does not exist 
     if (!mediaStorageDir.exists()) { 
      if (!mediaStorageDir.mkdirs()) { 
       Log.d("MicroFilm", "failed to create directory"); 
       return null; 
      } 
     } 

     // Create a media file name 
     String timeStamp; 
     cal = Calendar.getInstance(); 
     timeStamp = "" + cal.get(Calendar.YEAR) + cal.get(Calendar.MONTH) + 1 
       + cal.get(Calendar.DAY_OF_MONTH) + "_" 
       + cal.get(Calendar.HOUR_OF_DAY) + cal.get(Calendar.MINUTE) 
       + cal.get(Calendar.SECOND); 
     File mediaFile; 
     if (type == MEDIA_TYPE_IMAGE) { 
      mediaFile = new File(mediaStorageDir.getPath() + File.separator 
        + "IMG_" + timeStamp + ".jpg"); 
     } else if (type == MEDIA_TYPE_VIDEO) { 
      mediaFile = new File(mediaStorageDir.getPath() + File.separator 
        + "VID_" + timeStamp + ".mp4"); 
     } else { 
      return null; 
     } 

     return mediaFile; 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 
       // Image captured and saved to fileUri specified in the Intent 
       Toast.makeText(MicroFilm.this, "Image saved", Toast.LENGTH_LONG).show(); 
      } else if (resultCode == RESULT_CANCELED) { 
       finish(); 
      } else { 
       // Image capture failed, advise user 
      } 
     } 
    } 

} 

感謝您的幫助:)

+0

你可以把完整的堆棧跟蹤? –

+0

什麼是完整的堆棧跟蹤? –

回答

0

我認爲你的NullPointerException是拋出在captureSound()方法。

在開始調用此對象上的方法之前,您必須將值初始化爲變量soundRecorder

+0

感謝您的幫助。問題解決了 :) –

0

我建議你分配一些初值爲所有的實例變量。

當您在調用方法時將NULL對象作爲引用傳遞時,會出現NULLPOINTEREXCEPTION。

很可能是由於您沒有初始化這些變量。

例如,

private Uri imageUri; 

喜歡的東西

private Uri imageUri = new Uri(); 

(這只是一個例子,也許不是Uri類的正確初始化您的特定用途)

+0

感謝您的幫助。正如你所說的那樣,問題在於沒有初始化的soundRecorder。我不能給+,因爲我沒有很小的聲望! ;) –

+0

沒問題。只要確保在使用它之前初始化每個變量! – user25409

0

只需在setContentView(R.layout.activity_micro_film);後面寫soundRecorder=new MediaRecorder soundRecorder();即可。

相關問題