別のクラスからこのRecord.java
クラスを呼び出してIntent
からをクリックするとbutton1
、強制終了の例外が発生します。なぜこれが起こっているのか知りたいです。また、logcatは、デバイスが切断された「メディアプレーヤーを作成できません」などと言っています
コードは次のとおりです。
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;
}
});
}
}
これがlogcatの出力です
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)