title: unfortunately application has stopped?
body: <p>I'm working on an android application, and there are no errors anywhere, but when i run it on the mobile phone, after the application opens and i click on a button, after a while it suddenly closes and gives me the message "unfortunately ** has stopped".</p>
  
  <p>can someone please tell me what's wrong?</p>
  
  <p>I did some research and found that this message comes when there is something wrong in the manifest. Here is my manifest, i cant figure out what's wrong?</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt;
   &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.tryagain"
   android:versionCode="1"
   android:versionName="1.0" &gt;
  
  &lt;uses-sdk
  android:minSdkVersion="8"
  android:targetSdkVersion="17" /&gt;
  
  &lt;application
  android:allowBackup="true"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/AppTheme" &gt;
  &lt;activity
      android:name=".MainActivity"
      android:label="@string/app_name" &gt;
      &lt;intent-filter&gt;
          &lt;action android:name="android.intent.action.MAIN" /&gt;
  
          &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
      &lt;/intent-filter&gt;
     &lt;/activity&gt;
     &lt;/application&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
     &lt;uses-permission android:name="android.permission.RECORD_AUDIO" /&gt;
  
     &lt;/manifest&gt;
  </code></pre>
  
  <p>here is my .java : </p>
  
  <pre><code>public class MainActivity extends Activity {
  private static final int RECORDER_SAMPLERATE = 8000;
  private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
  private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
  private AudioRecord recorder = null;
  private Thread recordingThread = null;
  private boolean isRecording = false;
  ArrayList &lt;Short&gt; x=new ArrayList&lt;Short&gt;();
  
  ArrayList &lt;Short&gt; y=new ArrayList&lt;Short&gt;();
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  setButtonHandlers();
  enableButtons(false);
  
  int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
          RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
  
  //why is the following statement here???
  System.out.println("BUFFER SIZE VALUE IS " + bufferSize);
  
  }
  
   private void setButtonHandlers() {
  ((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick);
  ((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick);
  ((Button) findViewById(R.id.Start2)).setOnClickListener(btnClick2);
  ((Button) findViewById(R.id.Stop2)).setOnClickListener(btnClick2);
  
  
   }
  
   private void enableButton(int id, boolean isEnable) {
   ((Button) findViewById(id)).setEnabled(isEnable);
   }
  
  private void enableButtons(boolean isRecording) {
  enableButton(R.id.btnStart, !isRecording);
  enableButton(R.id.btnStop, isRecording);
  enableButton(R.id.Start2, !isRecording);
  enableButton(R.id.Stop2, isRecording);
  
   }
  
  int BufferElements2Rec = 1024; 
  
   int BytesPerElement = 2; // 2 bytes in 16bit format
  
   private void startRecording() {
  
  recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
          RECORDER_SAMPLERATE, RECORDER_CHANNELS,
          RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement);
  
  recorder.startRecording();
  
  isRecording = true;
  
  recordingThread = new Thread(new Runnable() {
  
      public void run() {
  
          writeAudioDataToFile();
  
      }
   }, "AudioRecorder Thread");
   recordingThread.start();
    }
  
  
    private void startRecording2() {
  
    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
          RECORDER_SAMPLERATE, RECORDER_CHANNELS,
          RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement);
  
     recorder.startRecording();
     isRecording = true;
  
    recordingThread = new Thread(new Runnable() {
  
      public void run() {
  
          writeAudioDataToFile2();
  
      }
     },  "AudioRecorder Thread");
    recordingThread.start();
      }
  
  
  private byte[] short2byte(short[] sData) {
  int shortArrsize = sData.length;
  byte[] bytes = new byte[shortArrsize * 2];
  
  for (int i = 0; i &lt; shortArrsize; i++) {
      bytes[i * 2] = (byte) (sData[i] &amp; 0x00FF);
      bytes[(i * 2) + 1] = (byte) (sData[i] &gt;&gt; 8);
      sData[i] = 0;
  }
  return bytes;
  }
  
    private void writeAudioDataToFile() {
  // Write the output audio in byte
  
  String filePath = "/storage/sdcard0/Sounds/recording1.pcm";
  
  
  
  short sData[] = new short[BufferElements2Rec];
  // to convert sData from short to Short:
  
  
  FileOutputStream os = null;
  try {
      os = new FileOutputStream(filePath);
  } catch (FileNotFoundException e) {
      e.printStackTrace();
  }
  
  while (isRecording) {
      // gets the voice output from microphone to byte format and stores it in buffer sData
  
      recorder.read(sData, 0, BufferElements2Rec);
  // here u will write the new code: to store what was said in an array,,, here we are 
      Short sData1= sData[BufferElements2Rec];
      int i=0; 
      x.add(i,sData1);
      i++;
  
      System.out.println("Short wirting to file" + sData.toString());
      try {
          // // writes the data to file from buffer
          // // stores the voice buffer
  
          byte bData[] = short2byte(sData);
  
          os.write(bData, 0, BufferElements2Rec * BytesPerElement);
      } 
  
      catch (IOException e) {
          e.printStackTrace();
      }
       }
  
    try {
      os.close();
     } catch (IOException e) {
      e.printStackTrace();
  }
  } //END OF WRITE TO FILE METHOD
  
  
  private void writeAudioDataToFile2() {
  // Write the output audio in byte
  
  String filePath = "/storage/sdcard0/Sounds/recording2.pcm";
  
  
  
  short sData[] = new short[BufferElements2Rec];
  // to convert sData from short to Short:
  
  
  
  FileOutputStream os = null;
  try {
      os = new FileOutputStream(filePath);
  } catch (FileNotFoundException e) {
      e.printStackTrace();
  }
  
  while (isRecording) {
      // gets the voice output from microphone to byte format and stores it in buffer sData
  
      recorder.read(sData, 0, BufferElements2Rec);
  // here u will write the new code: to store what was said in an array,,, here we are 
      Short sData1= sData[BufferElements2Rec];
      int i=0; 
      y.add(i,sData1);
      i++;
  
      System.out.println("Short wirting to file" + sData.toString());
      try {
          // // writes the data to file from buffer
          // // stores the voice buffer
  
          byte bData[] = short2byte(sData);
  
          os.write(bData, 0, BufferElements2Rec * BytesPerElement);
      } 
  
      catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  try {
      os.close();
  } catch (IOException e) {
      e.printStackTrace();
  }
  } //END OF WRITE TO FILE METHOD
  
  
   private void stopRecording() {
  // stops the recording activity
  if (null != recorder) {
      isRecording = false;
  
      recorder.stop();
      recorder.release();
  
      recorder = null;
      recordingThread = null;
  }
  }
  
  private View.OnClickListener btnClick = new View.OnClickListener() {
  public void onClick(View v) {
      switch (v.getId()) {
      case R.id.btnStart: {
          enableButtons(true);
          startRecording();
          break;
      }
      case R.id.btnStop: {
          enableButtons(false);
          stopRecording();
          break;
      }
      }
      }
      };
  
     private View.OnClickListener btnClick2 = new View.OnClickListener() {
      public void onClick(View v) {
      switch (v.getId()) {
      case R.id.Start2: {
          enableButtons(true);
          startRecording2();
          break;
      }
      case R.id.Stop2: {
          enableButtons(false);
          stopRecording();
          break;
      }
      }
     }
     };
  
  
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
  
    if (keyCode == KeyEvent.KEYCODE_BACK) {
  
      finish();
     }
       return super.onKeyDown(keyCode, event);
    }
    }
  </code></pre>
  
  <p>here is my logCat</p>
  
  <pre><code>   06-27 16:50:46.830: I/System.out(11939): BUFFER SIZE VALUE IS 2048
     06-27 16:50:46.980: D/libEGL(11939): loaded /vendor/lib/egl/libGLES_vc4.so
     06-27 16:50:46.980: D/(11939): SpyHook inactive - could not find libspyhook.so [Symbol not      found: ] or not enabled (debug.egl.hw.spy = 0)
     06-27 16:50:47.000: W/khrn_client(11939): init_window num_buffers 3 min undequeued buffers 1 type 1
     06-27 16:50:47.000: W/khrn_client(11939): init_window window 0x50eda0d8, 480x800 hintTransform 0x0
     06-27 16:50:47.030: D/OpenGLRenderer(11939): Enabling debug mode 0
     06-27 16:50:53.066: W/dalvikvm(11939): threadid=12: thread exiting with uncaught exception (group=0x40fb72a0)
     06-27 16:50:53.066: E/AndroidRuntime(11939): FATAL EXCEPTION: AudioRecorder Thread
     06-27 16:50:53.066: E/AndroidRuntime(11939): java.lang.ArrayIndexOutOfBoundsException:     length=1024; index=1024 
    06-27 16:50:53.066: E/AndroidRuntime(11939):  at        com.example.tryagain.MainActivity.writeAudioDataToFile(MainActivity.java:148)
    06-27 16:50:53.066: E/AndroidRuntime(11939):  at com.example.tryagain.MainActivity.access$4(MainActivity.java:125)
   06-27 16:50:53.066: E/AndroidRuntime(11939):   at         com.example.tryagain.MainActivity$3.run(MainActivity.java:84)
   06-27 16:50:53.066: E/AndroidRuntime(11939):   at java.lang.Thread.run(Thread.java:856)
  </code></pre>