2013-03-16 72 views
0

我的應用程序具有很多功能,在我的應用程序中添加了相機功能後,如果單擊相機按鈕,我的應用程序意外停止未來。
AndroidManifest.xml單擊相機按鈕,拍攝照片後,應用程序已停止在我的設備中

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.Ibetter.tdm" 
     android:versionCode="1" 
     android:versionName="1.0" > 

     <uses-sdk 
      android:minSdkVersion="8" 
      android:targetSdkVersion="17" 
      />  
     <uses-permission android:name="android.permission.WAKE_LOCK" ></uses-permission> 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission> 
     <uses-permission android:name="android.permission.INTERNET" /> 
     <uses-feature android:name="android.hardware.camera" /> 
     <uses-permission android:name="android.permission.CAMERA" /> 
     <uses-feature android:name="android.hardware.camera.autofocus" /> 

     <application 
      android:allowBackup="true" 
      android:icon="@drawable/icon" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme" > 
      <activity 
       android:name="com.Ibetter.tdm.ProjectListActivity" 
       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="com.Ibetter.tdm.ActivityTab" 
       android:label="@string/title_activity_tab_edit" 
       android:parentActivityName="com.Ibetter.tdm.ProjectListActivity"> 
      </activity> 
     <activity 
       android:name="com.Ibetter.tdm.ProjectEditActivity" 
       android:label="@string/title_activity_project_edit" > 
    </activity> 
    <activity 
     android:name="com.Ibetter.tdm.ReleaseEditActivity" 
     android:label="@string/title_activity_release_edit" > 
    </activity> 
    <activity 
     android:name="com.Ibetter.tdm.ReleaseListActivity" 
     android:label="@string/title_activity_release_list" > 
    </activity> 
    <activity 
     android:name="com.Ibetter.tdm.TestEditActivity" 
     android:label="@string/title_activity_test_edit" > 
    </activity> 
    <activity 
     android:name="com.Ibetter.tdm.TestListActivity" 
     android:label="@string/title_activity_test_list" > 
    </activity> 
    <activity 
     android:name="com.Ibetter.tdm.ReqListActivity" 
     android:label="@string/title_activity_req_list" > 
    </activity> 
    <activity 
     android:name="com.Ibetter.tdm.ReqEditActivity" 
     android:label="@string/title_activity_req_edit" > 
    </activity> 
      </application> 
    </manifest> 

這是相機功能的Java文件..

public class ReqEditActivity extends Activity implements OnCompletionListener { 
     private EditText mTitleText; 
     private EditText mReqDescText; 
     private EditText mReqStepsText; 
     private Button mConfirmButton; 
     private Long mRowId; 
     private ProjectsDbAdapter mDbHelper; 
     private String gprojectname =""; 
     private String greleasename =""; 
     private int modifyFormat; 
     private static final int IMAGE_CAPTURE = 1337; 
     private Button startBtn; 
     private Uri imageUri; 
     private ImageView imageView; 
     //private static int RESULT_LOAD_IMAGE = 1; 
     private static final int REQUEST_CODE = 1; 
     private Bitmap bitmap; 
     //private ImageView imageView; 
     private static final int SELECT_PICTURE = 1; 
     //private String selectedImagePath; 
     private ImageButton btnPlay; 
     // Media Player 
     private MediaPlayer mp; 
     private String gfilename1 =""; 
     private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp"; 
     private static final String AUDIO_RECORDER_FILE_EXT_AMR = ".amr"; 
    private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4"; 
    private static final String REQAUDIO_RECORDER_FOLDER = "ReqAudioRecorder";  
    private MediaRecorder recorder = null; 
public static int currentFormat = 0; 
private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4, MediaRecorder.OutputFormat.THREE_GPP, MediaRecorder.OutputFormat.AMR_NB}; 
private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP, AUDIO_RECORDER_FILE_EXT_AMR }; 
protected static final int RESULT_SPEECH_DESC = 1; 
protected static final int RESULT_SPEECH_STEPS = 2; 
     private ImageButton btnSpeakdesc; 
    private ImageButton btnSpeaksteps; 
    @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
     mDbHelper = new ProjectsDbAdapter(this); 
    setContentView(R.layout.activity_req_edit); 
    mTitleText = (EditText) findViewById(R.id.title); 
    mReqDescText = (EditText) findViewById(R.id.body); 
    mReqStepsText = (EditText) findViewById(R.id.Steps); 
    mConfirmButton = (Button) findViewById(R.id.confirm); 
    Bundle extras = getIntent().getExtras(); 
    gprojectname = extras.getString("project"); 
    greleasename = extras.getString("release"); 
    startBtn = (Button) findViewById(R.id.startBtn); 
     startBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       try { 
        startCamera(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }); 
     //retrieve a reference to the ImageView 
     //ImageView picView = (ImageView)findViewById(R.id.picture); 
     //Bitmap thePic = null; 
     //display the returned cropped image 
     // picView.setImageBitmap(thePic);  
    mRowId = savedInstanceState != null ? savedInstanceState.getLong(ProjectsDbAdapter.KEY_ROWID_REQS): null; 
    registerButtonListenersAndSetDefaultText(); 
    //for speech to text and recording purpose   
    setButtonHandlers(); 
    enableButtons(false); 
    btnSpeakdesc = (ImageButton) findViewById(R.id.btnSpeakdesc);  
    btnSpeaksteps = (ImageButton) findViewById(R.id.btnSpeaksteps); 
    // All player buttons 
    btnPlay = (ImageButton) findViewById(R.id.btnPlay);     
    // Mediaplayer 
    mp = new MediaPlayer();  
    btnSpeakdesc.setOnClickListener(new View.OnClickListener() 
    {     
    @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(
        RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
      intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US"); 
      try { 
       startActivityForResult(intent, RESULT_SPEECH_DESC); 
       mReqDescText.setText(""); 
      } 
      catch (ActivityNotFoundException a) 
      { 
       Toast t = Toast.makeText(getApplicationContext(), 
         "Opps! Your device doesn't support Speech to Text", 
         Toast.LENGTH_SHORT); 
       t.show(); 
      } 
     } 
    }); 
    btnSpeaksteps.setOnClickListener(new View.OnClickListener() 
    {     
    @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(
        RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
      intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US"); 
      try { 
       startActivityForResult(intent, RESULT_SPEECH_STEPS); 
       mReqStepsText.setText(""); 
      } 
      catch (ActivityNotFoundException a) 
      { 
       Toast t = Toast.makeText(getApplicationContext(), 
         "Opps! Your device doesn't support Speech to Text", 
         Toast.LENGTH_SHORT); 
       t.show(); 
      } 
     } 
    }); 
    /** 
    * Play button click event 
    * plays a song and changes button to pause image 
    * pauses a song and changes button to play image 
    * */ 
    btnPlay.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View arg0) 
     { 
      // check for already playing 
      if(mp.isPlaying()){ 
       if(mp!=null){ 
        mp.pause(); 
        // Changing button image to play button 
        btnPlay.setImageResource(R.drawable.btn_play); 
       } 
      }else{ 
       // Resume song 
       if(mp!=null){ 
        mp.start(); 
        // Changing button image to pause button 
        btnPlay.setImageResource(R.drawable.btn_pause); 
        // play selected song 
        Cursor filename = mDbHelper.fetchreqsFileName(mRowId); 
        startManagingCursor(filename); 
        gfilename1 = filename.getString(filename.getColumnIndexOrThrow(ProjectsDbAdapter.KEY_REQFILENAME)); 
        playSong(gfilename1); 
       } 
      } 
     } 
    }); 
} 
    public void startCamera() 
     throws IOException { 
    Log.d("QAM_CAMERA", "Starting camera on the phone..."); 
    File photosDir = new File(Environment.getExternalStorageDirectory(), "photos"); 
    if (!photosDir.isDirectory()) { 
     photosDir.mkdirs(); 
    } 
    File imageFile = File.createTempFile("testphoto", ".jpg", photosDir); 
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile)); 
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 
    startActivityForResult(intent, IMAGE_CAPTURE); 
} 
    private void setRowIdFromIntent() { 
    if (mRowId == null || mRowId.longValue() == 0) { 
     Bundle extras = getIntent().getExtras();    
     gprojectname = extras.getString("project"); 
     greleasename = extras.getString("release"); 
     mRowId = extras != null ? extras.getLong(ProjectsDbAdapter.KEY_ROWID_REQS) : null; 
    } 
    } 
     @Override 
     protected void onPause() { 
      super.onPause(); 
      mDbHelper.close(); 
     } 
     @Override 
     protected void onResume() { 
      super.onResume(); 
      mDbHelper.open(); 
     setRowIdFromIntent(); 
    populateFields(); 
     } 
     private void registerButtonListenersAndSetDefaultText() { 
      mConfirmButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
    public void onClick(View view) { 
       saveState(); 
       setResult(RESULT_OK); 
       Toast.makeText(ReqEditActivity.this, getString(R.string.task_saved_message), Toast.LENGTH_SHORT).show(); 
       finish(); 
      } 
      }); 
     } 
     private void populateFields() { 
    try 
    { 
     // Only populate the text boxes and change the calendar date 
    // if the row is not null from the database. 
     if (mRowId != null) 
    { 
       Cursor reqs = mDbHelper.fetchreqs(mRowId); 
       startManagingCursor(reqs); 
       mTitleText.setText(reqs.getString(
      reqs.getColumnIndexOrThrow(ProjectsDbAdapter.KEY_TITLE))); 
     mReqDescText.setText(reqs.getString(
       reqs.getColumnIndexOrThrow(ProjectsDbAdapter.KEY_DESC))); 
     mReqStepsText.setText(reqs.getString(
       reqs.getColumnIndexOrThrow(ProjectsDbAdapter.KEY_BODY))); 
     reqs.close(); 
    } else { 
      // This is a new task - add defaults from preferences if set. 
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
      String defaultTitleKey = getString(R.string.pref_task_title_key);   
      String defaultTitle = prefs.getString(defaultTitleKey, null);  
      //if(defaultTitle != null) 
     mTitleText.setText("Edit REQS"); 
      } 
    } 
    catch (Exception ex) 
    {} 
     } 
     @Override 
     protected void onSaveInstanceState(Bundle outState) { 
      super.onSaveInstanceState(outState); 
      outState.putLong(ProjectsDbAdapter.KEY_ROWID_REQS, mRowId); 
     } 
     private void saveState() { 
      String title = mTitleText.getText().toString(); 
      String Desc = mReqDescText.getText().toString(); 
      String Body = mReqStepsText.getText().toString(); 
      if (mRowId == null || mRowId.longValue() == 0) 
      { 
      long id = mDbHelper.createreqs(title, Desc, Body,gprojectname,greleasename); 
       if (id > 0) { 
        mRowId = id; 
       } 
      } else { 
       mDbHelper.updatereqs(mRowId, title, Desc, Body); 
      } 
     } 
    //for speech to text and audio recording 
    public View.OnClickListener btnClick = new View.OnClickListener() 
     { 
     @Override 
    public void onClick(View v) { 
     switch(v.getId()){ 
      case R.id.btnStart:{ 
       AppLog.logString("Start Recording"); 
       enableButtons(true); 
       startRecording(); 
       break; 
      } 
      case R.id.btnStop:{ 
       AppLog.logString("Start Recording"); 
       enableButtons(false); 
       stopRecording(); 
            break; 
      } 
      } 
    } 
    }; 
public MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() { 
    @Override 
    public void onError(MediaRecorder mr, int what, int extra) { 
     AppLog.logString("Error: " + what + ", " + extra); 
    } 
}; 
public MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() { 
    @Override 
    public void onInfo(MediaRecorder mr, int what, int extra) { 
     AppLog.logString("Warning: " + what + ", " + extra); 
    } 
}; 
public void setButtonHandlers() { 
    ((Button)findViewById(R.id.btnStart)).setOnClickListener(btnClick); 
    ((Button)findViewById(R.id.btnStop)).setOnClickListener(btnClick); 
} 
public void enableButton(int id,boolean isEnable){ 
    ((Button)findViewById(id)).setEnabled(isEnable); 
} 
public void enableButtons(boolean isRecording) { 
    enableButton(R.id.btnStart,!isRecording);   
    enableButton(R.id.btnStop,isRecording); 
} 
/*private void setStopButtonCaption(){ 
    ((Button)findViewById(R.id.btnStop)).setText(getString(R.string.audio_format) + " (" + file_exts[currentFormat] + ")"); 
}*/ 
@SuppressLint("NewApi") 
private void startRecording(){ 
    //displayFormatDialog(); 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    String formats[] = {"MPEG 4", "3GPP", "AMR"}; 
    builder.setTitle(getString(R.string.choose_format_title)) 
.setSingleChoiceItems(formats, currentFormat, new DialogInterface.OnClickListener()     { 

    @Override 
    public void onClick(DialogInterface dialog, int which) { 
    currentFormat = which; 
    dialog.dismiss(); 
    recorder = new MediaRecorder();   
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    recorder.setOutputFormat(output_formats[currentFormat]); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
    recorder.setOutputFile(getFilename());   
    //recorder.setOnErrorListener(errorListener); 
    //recorder.setOnInfoListener(infoListener); 
    try { 
     recorder.prepare(); 
     recorder.start(); 
    } catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 
    }).show(); 
} 
private void stopRecording(){ 
    if(null != recorder) 
    { 
     mDbHelper.updatereqsFileName(mRowId, gfilename1); 
     recorder.stop(); 
     recorder.reset(); 
     recorder.release();    
     recorder = null; 
     } 
    } 
    /*private int displayFormatDialog(){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    String formats[] = {"MPEG 4", "3GPP", "AMR"}; 
    builder.setTitle(getString(R.string.choose_format_title)) 
      .setSingleChoiceItems(formats, currentFormat, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       currentFormat = which; 
       dialog.dismiss(); 
      } 
      }).show(); 
    return currentFormat; 
}*/ 
public String getFilename(){ 
    String filepath = Environment.getExternalStorageDirectory().getPath(); 
    File file = new File(filepath,REQAUDIO_RECORDER_FOLDER); 
    if(!file.exists()){ 
     file.mkdirs(); 
    } 
    gfilename1 = (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]); 
    return (gfilename1); 
} 
    /** 
     * Receiving song index from playlist view 
     * and play the song 
     * */ 
     @Override 
     protected void onActivityResult(int requestCode, 
           int resultCode, Intent data) { 
      super.onActivityResult(requestCode, resultCode, data); 
    //conversion and audio record 
     switch (requestCode) 
      { 
     case RESULT_SPEECH_DESC: { 
      if (resultCode == RESULT_OK && null != data) { 
       ArrayList<String> text = data 
         .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
       mReqDescText.setText(text.get(0)); 
      } 
      break; 
     } 
     case RESULT_SPEECH_STEPS: { 
      if (resultCode == RESULT_OK && null != data) { 
       ArrayList<String> text = data 
         .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
       mReqStepsText.setText(text.get(0)); 
      } 
      break; 
     } 
    } 
    if (requestCode == IMAGE_CAPTURE) { 
     if (resultCode == RESULT_OK){ 
      Log.d("ANDRO_CAMERA","Picture taken!!!"); 
      imageView.setImageURI(imageUri); 
     } 
    } 
} 
    @Override 
public void onCompletion (MediaPlayer arg0) 
{    
} 
     /** 
     * Function to play a song 
     * @param songIndex - index of song 
     * */ 
     public void playSong(String gfilename1){ 
      // Play song 
      try 
      { 
       mp.reset(); 
       mp.setDataSource(gfilename1); 
       mp.prepare(); 
       mp.start(); 
       // Changing Button Image to pause image 
       btnPlay.setImageResource(R.drawable.btn_pause); 
      } catch (IllegalArgumentException e) { 
       e.printStackTrace(); 
      } catch (IllegalStateException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
     } 
     } 
    @Override 
     public void onDestroy(){ 
     super.onDestroy(); 
      mp.release(); 
     } 
    public String getPath(Uri uri) { 
      String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(uri, projection, null, null, null); 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } 
    } 

加入拍照功能之後,這些錯誤顯示,你可以看看我的Java和清單文件,是任何版本問題在後面?

logcat的錯誤

03-16 04:48:06.826: E/AndroidRuntime(1239): FATAL EXCEPTION: main 
    03-16 04:48:06.826: E/AndroidRuntime(1239): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException 
    03-16 04:48:06.826: E/AndroidRuntime(1239):  at android.app.LoadedApk.makeApplication(LoadedApk.java:504) 
    03-16 04:48:06.826: E/AndroidRuntime(1239):  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4364) 
    03-16 04:48:06.826: E/AndroidRuntime(1239):  at android.app.ActivityThread.access$1300(ActivityThread.java:141) 
    03-16 04:48:06.826: E/AndroidRuntime(1239):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294) 
    03-16 04:48:06.826: E/AndroidRuntime(1239):  at android.os.Handler.dispatchMessage(Handler.java:99) 
    03-16 04:48:06.826: E/AndroidRuntime(1239):  at android.os.Looper.loop(Looper.java:137) 
    03-16 04:48:06.826: E/AndroidRuntime(1239):  at android.app.ActivityThread.main(ActivityThread.java:5039) 
    03-16 04:48:06.826: E/AndroidRuntime(1239):  at java.lang.reflect.Method.invokeNative(Native Method) 
    03-16 04:48:06.826: E/AndroidRuntime(1239):  at java.lang.reflect.Method.invoke(Method.java:511) 
    03-16 04:48:06.826: E/AndroidRuntime(1239):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    03-16 04:48:06.826: E/AndroidRuntime(1239):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
    03-16 04:48:06.826: E/AndroidRuntime(1239):  at dalvik.system.NativeStart.main(Native Method) 
    03-16 04:48:06.826: E/AndroidRuntime(1239): Caused by: java.lang.NullPointerException 
    03-16 04:48:06.826: E/AndroidRuntime(1239):  at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:379) 
    03-16 04:48:06.826: E/AndroidRuntime(1239):  at android.app.LoadedApk.getClassLoader(LoadedApk.java:322) 
    03-16 04:48:06.826: E/AndroidRuntime(1239):  at android.app.LoadedApk.makeApplication(LoadedApk.java:496) 
    03-16 04:48:06.826: E/AndroidRuntime(1239):  ... 11 more 
+1

除了最重要的部分,您似乎已經逐字添加了所有內容。 logcat堆棧跟蹤!如果我可以添加,也會格式錯誤。請僅將您的代碼修剪至相關部分。 – 2013-03-16 04:29:30

+0

雅我把logcar錯誤 – kkarthickk 2013-03-16 04:32:55

+0

你可以發佈你從哪裏調用這個活動的代碼片段。 – 2013-03-16 04:59:57

回答

0

看來你的包管理器沒有得到它想要的信息。使用ADT/adb/eclipse的已知錯誤。你可以做以下給他們一個嘗試。大部分它會解決你的問題。

  • 嘗試清理您的項目 - >從設備/模擬器卸載應用程序 - >再次運行它。

  • 如果您正在使用任何庫,那麼在進入項目屬性後嘗試在Project中給出該庫的引用。

  • 嘗試使用另一個新創建的工作區。

希望它會幫助。

相關問題