2012-07-05 2 views
0

다른 클래스의 Intent을 통해 Record.java 클래스를 호출 한 다음 button1을 클릭하면 강제 종료 예외가 발생합니다. 왜 이런 일이 일어나고 있는지 알고 싶습니다. 여기 이 레코드 클래스를 호출 할 때 강제 종료 close

public class Record extends Activity{ 
    Button btn1,btn2; 
    MediaRecorder recorder; 
    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.record); 
      btn1=(Button)findViewById(R.id.button1); 
      btn2=(Button)findViewById(R.id.button2); 
      btn1.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        recorder = new MediaRecorder(); 

        recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
        recorder.setOutputFile("/sdcard/sample.3gp"); 


        try { 
         recorder.prepare(); 
         recorder.start(); 
        } catch (IllegalStateException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 
      btn2.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        recorder.stop(); 
        recorder.reset(); 
        recorder.release(); 

        recorder = null; 
       } 
      }); 
    } 

} 

07-05 10:20:07.598: E/AndroidRuntime(841): Uncaught handler: thread main exiting due to uncaught exception 
07-05 10:20:07.609: E/AndroidRuntime(841): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newaa/com.example.newaa.Recipient}: java.lang.IndexOutOfBoundsException: charAt: -1 < 0 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.os.Looper.loop(Looper.java:123) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.ActivityThread.main(ActivityThread.java:4363) 
07-05 10:20:07.609: E/AndroidRuntime(841): at java.lang.reflect.Method.invokeNative(Native Method) 
07-05 10:20:07.609: E/AndroidRuntime(841): at java.lang.reflect.Method.invoke(Method.java:521) 
07-05 10:20:07.609: E/AndroidRuntime(841): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
07-05 10:20:07.609: E/AndroidRuntime(841): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
07-05 10:20:07.609: E/AndroidRuntime(841): at dalvik.system.NativeStart.main(Native Method) 
07-05 10:20:07.609: E/AndroidRuntime(841): Caused by: java.lang.IndexOutOfBoundsException: charAt: -1 < 0 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.text.SpannableStringBuilder.charAt(SpannableStringBuilder.java:110) 
07-05 10:20:07.609: E/AndroidRuntime(841): at com.example.newaa.Recipient$2.onTextChanged(Recipient.java:76) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.widget.TextView.sendOnTextChanged(TextView.java:6131) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.widget.TextView.setText(TextView.java:2687) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.widget.TextView.setText(TextView.java:2552) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.widget.EditText.setText(EditText.java:71) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.widget.TextView.setText(TextView.java:2527) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.widget.TextView.onRestoreInstanceState(TextView.java:2427) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.view.View.dispatchRestoreInstanceState(View.java:5940) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1127) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1127) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.view.View.restoreHierarchyState(View.java:5919) 
07-05 10:20:07.609: E/AndroidRuntime(841): at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1454) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.Activity.onRestoreInstanceState(Activity.java:835) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.Activity.performRestoreInstanceState(Activity.java:807) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096) 
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2473) 
+0

logcat 출력을 게시하십시오. – Rajesh

+0

@Rajesh 나는 그것을 지금했다. –

+0

FC를 언제 구합니까? 활동을 시작하거나 버튼을 누르는 동안? FC를 일으키는 Exception의 스택 추적이 없습니다. com.example.newaa.Record'에 대한 스택 트레이스를 포함하는 전체 logcat 출력을 게시하십시오. – Rajesh

답변

1

문제는 com.example.newaa.Recipient 클래스에 있습니다.
이 줄 번호를 참조하십시오. com.example.newaa.Recipient $ 2.onTextChanged (Recipient.java:76)에서
당신이 문제를 찾거나 재사용처럼 Recipient.java

+0

아니요. 오류없이 Record.java를 호출 할 때 오류가 Recipient.java에있을 수 있습니까? –

+0

logcat에 따라 : 발생 원인 : java.lang.IndexOutOfBoundsException : charAt : -1 <0 07-05 10 : 20 : 07.609 : E/AndroidRuntime (841) : android.text.SpannableStringBuilder.charAt (SpannableStringBuilder. java : 110) 07-05 10 : 20 : 07.609 : E/AndroidRuntime (841) : com.example.newaa.Recipient $ 2.onTextChanged (Recipient.java:76) – Waqas

+0

아마도 이전 로그를 붙여 넣었을 것입니다. 최신 로그를 붙여 넣었는지 확인하십시오. – Waqas

0

당신은 출력을 호출하지 말아야 로그 캣의 출력 : 또한, 로그 캣이 끊어 장치 같은

코드는 "수없는 미디어 플레이어를 만들 수"라는 그런 파일. 대신 사용하십시오 Environment.getExternalStorageDirectory()

+0

정확한 형식을 사용할 수 있습니까? –

+0

이렇게하면됩니다. 파일 file = 새 파일 (Environment.getExternalStorageDirectory() \t \t \t \t .getAbsolutePath() + "/temp.pcm"); – Waqas

1

만들기 코드의 코드를 게시 언제 어디서나 바랍니다. Context ref를 사용하여 mediarecorder를 직접 호출하는 코드입니다. 그것을 다루는 방법도 사용하십시오.

public class AudioRecorder { 
    final MediaRecorder recorder = new MediaRecorder(); 
    final String path; 
    Context context; 

    public AudioRecorder(Context context,String path) { 
    this.context = context; 
    this.path = sanitizePath(path); 
    } 

    private String sanitizePath(String path) { 
    if (!path.startsWith("/")) { 
     path = "/" + path; 
    } 
    if (!path.contains(".")) { 
     path += ".3gp"; 
    } 
    return Environment.getExternalStorageDirectory().getAbsolutePath() + path; 
    } 
    /** 
    * Starts a new recording. 
    */ 
    public void start() throws IOException { 
    String state = android.os.Environment.getExternalStorageState(); 
    if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) { 
     Toast.makeText(context,"SD Card is not mounted",Toast.LENGTH_LONG).show(); 
     throw new IOException("SD Card is not mounted. It is " + state + "."); 

    } 
    // make sure the directory we plan to store the recording in exists 
    File directory = new File(path).getParentFile(); 
    if (!directory.exists() && !directory.mkdirs()) { 
     throw new IOException("Path to file could not be created."); 
    } 
    System.out.println("Full path of audio"+path); 

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
    recorder.setOutputFile(path); 
    recorder.prepare(); 
    recorder.start(); 


    } 
    /** 
    * Stops a recording that has been previously started. 
    */ 
    public void stop() throws IOException { 
    recorder.stop(); 
    recorder.release(); 
    } 

    public void Release() throws IOException{ 
     recorder.release(); 
    } 

}** 
0

당신이 너무 오래 전에서 my answer to myself에서 볼 수 있듯이, 안드로이드는 비디오 캡처 의도에 EXTRA_OUTPUT 플래그를 설정에 문제가 있습니다. 대신 기본 파일 위치를 가져 와서 다음과 같이 비디오를 복사하십시오.

protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    super.onActivityResult(requestCode, resultCode, intent); 

    if (resultCode == RESULT_OK) { 
     try { 
      AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r"); 
      FileInputStream fis = videoAsset.createInputStream(); 
      File videoFile = new File(Environment.getExternalStorageDirectory(),"<VideoFileName>.mp4"); 
      FileOutputStream fos = new FileOutputStream(videoFile); 

      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = fis.read(buffer)) > 0) { 
      fos.write(buffer, 0, length); 
     }  
     fis.close(); 
     fos.close(); 
     } catch (IOException e) { 
      // TODO: handle error 
     } 
    } 
} 
관련 문제