title: Start activity from fragment Android
body: <p>I am trying to start a new activity for recording audio. This activity is started when pressing a button (recordSong) in the fragment Settings. When I run the app on my phone, and press the button the app stops with the message "Sorry, the app has stopped" (translated from Swedish). </p>
  
  <p>This logcat is returned:</p>
  
  <pre><code>03-30 12:15:01.880 21959-21959/com.vkb.colium I/HWSERVICES: hwnative_get_component  comes...
  03-30 12:15:01.880 21959-21959/com.vkb.colium W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41740c50)
  03-30 12:15:03.705 21959-21959/com.vkb.colium I/Process: Sending signal. PID: 21959 SIG: 9
  </code></pre>
  
  <p>Here is the code for the method onCreateView() in the class Settings. The commented lines are code I found that might have helped me, but gives me the same error: </p>
  
  <pre><code> public class Settings extends Fragment {
  
      private Button recordSong;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
  
          View rootView = inflater.inflate(R.layout.settings_fragment, container, false);
  
          recordSong = (Button)rootView.findViewById(R.id.recordSongBtn);
          recordSong.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  System.out.println("The record song button was clicked.");
                  Intent intent = new Intent(getActivity(), RecordAudio.class);
                  getActivity().startActivity(intent);
                  //((MainActivity) getActivity()).startActivity(intent);
                  //startActivity(intent);
                  //Settings.this.startActivity(intent);
              }
          });
  
          return rootView;
      }
  }
  </code></pre>
  
  <p>Here is code for the class RecordAudio:</p>
  
  <pre><code>public class RecordAudio extends Activity {
  
  ImageButton record, stop, play, save;
  EditText name;
  private MediaRecorder audioRecorder;
  private String outputFile;
  Logic fsm = MainActivity.getFsm();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_record_audio);
  
      record = (ImageButton) findViewById(R.id.recordBtn);
      stop = (ImageButton) findViewById(R.id.stopBtn);
      play = (ImageButton) findViewById(R.id.playBtn);
  
      stop.setEnabled(false);
      play.setEnabled(false);
      outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp"; 
  
      audioRecorder = new MediaRecorder();
      audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      audioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
      audioRecorder.setOutputFile(outputFile);
  
      record.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // Turn off features of the ColiCot, so that it doesn't start when singing
              fsm.setPlayAudio(false);
              fsm.setPlayVideo(false);
              fsm.setRunMotor(false);
  
              try {
                  audioRecorder.prepare();
                  audioRecorder.start();
              }
              catch (IllegalStateException e) {
                  e.printStackTrace();
              }
              catch (IOException e) {
                  e.printStackTrace();
              }
  
              record.setEnabled(false);
              stop.setEnabled(true);
  
              Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_SHORT);
          }
      });
  
      stop.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // Turn on features of the ColiCot
              fsm.setPlayAudio(true);
              fsm.setPlayVideo(true);
              fsm.setRunMotor(true);
  
              audioRecorder.stop();
              audioRecorder.release();
              audioRecorder = null;
  
              stop.setEnabled(false);
              play.setEnabled(true);
  
              Toast.makeText(getApplicationContext(), "Your song has been recorded", Toast.LENGTH_SHORT);
          }
      });
  
      play.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              MediaPlayer mp = new MediaPlayer();
  
              try {
                  mp.setDataSource(outputFile);
              }
              catch (IOException e) {
                  e.printStackTrace();
              }
  
              try {
                  mp.prepare();
              }
              catch (IOException e) {
                  e.printStackTrace();
              }
  
              mp.start();
              Toast.makeText(getApplicationContext(), "Playing song", Toast.LENGTH_SHORT);
          }
      });
  
      save.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) { // Lægger in i egen fil på telefonen
              String path = FileHelper.getNextAudioFilePath();
              name = (EditText) findViewById(R.id.nameEditText);
              audioRecorder.setOutputFile(path + "/" + name);
          }
      });
  }
  
  @Override
  protected void onDestroy() { // Deletes recorded sound from temporary file when activity is closed
      super.onDestroy();
  
      // Turn on features of the ColiCot
      fsm.setPlayAudio(true);
      fsm.setPlayVideo(true);
      fsm.setRunMotor(true);
  
      File recordedAudio = new File(outputFile);
      boolean success = recordedAudio.delete();
      if (!success) {
          System.out.println("The recorded file was not deleted");
      }
  }
  
  }
  </code></pre>
  
  <p>And this is my AndroidManifest:</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.vkb.colium"
      android:versionCode="1"
      android:versionName="1.0"&gt;
  
  &lt;uses-sdk
      android:minSdkVersion="16"
      android:targetSdkVersion="19" /&gt;
  
  &lt;uses-feature android:name="android.hardware.usb.host" /&gt;
  
  &lt;uses-permission android:name="android.permission.RECORD_AUDIO" /&gt;
  &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  
  &lt;android:uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
  &lt;android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&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"
          android:screenOrientation="portrait"&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;activity
          android:name="com.vkb.colium.media.RecordAudio"
          android:label="@string/app_name"
          android:screenOrientation="portrait"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="com.vkb.colium.media.RECORDAUDIO" /&gt;
  
              &lt;category android:name="android.intent.category.DEFAULT"/&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <p>Here is the xml file for the activity that records the audio:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:orientation="vertical"
  tools:context="com.vkb.colium.media.RecordAudio2"&gt;
  
  &lt;LinearLayout
      android:id="@+id/relativeLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"&gt;
  
      &lt;ImageButton
          android:id="@+id/recordBtn"
          android:layout_width="70dp"
          android:layout_height="70dp"
          android:src="@drawable/record_button"
          android:background="@android:color/background_light"
          android:layout_weight="50"
          android:gravity="right" /&gt;
  
      &lt;ImageButton
          android:id="@+id/stopBtn"
          android:layout_width="30dp"
          android:layout_height="30dp"
          android:src="@drawable/stop_button"
          android:background="@android:color/background_light"
          android:layout_weight="50"
          android:gravity="left" /&gt;
  
  &lt;/LinearLayout&gt;
  
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:layout_below="@+id/relativeLayout"&gt;
  
  &lt;ImageButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/playBtn"
  
      android:src="@drawable/play_button"
      android:background="@android:color/background_light"/&gt;
  
  &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:text="Please enter a name for your song and click Save"
      android:id="@+id/name"
      android:layout_below="@+id/playBtn"
      android:layout_marginTop="45dp" /&gt;
  
      &lt;EditText
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/nameEditText" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Save"
          android:id="@+id/saveBtn"
          android:layout_below="@+id/textView"
          android:layout_marginTop="44dp" /&gt;
  
  &lt;/LinearLayout&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>And the xml file for the settings fragment:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"&gt;
  
  &lt;TextView
      android:id="@+id/alarmText"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentTop="true"
      android:text="Active Time 2 Minutes"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:layout_marginLeft="20dp"
      android:layout_marginRight="20dp"/&gt;
  
  &lt;SeekBar
      android:id="@+id/adjustTime"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@+id/alarmText"
      android:max="90"
      android:layout_marginLeft="20dp"
      android:layout_marginRight="20dp"/&gt;
  
  &lt;CheckBox
      android:id="@+id/enableAudioCheckBox"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@+id/adjustTime"
      android:text="Enable Audio"
      android:layout_marginLeft="20dp"
      android:layout_marginRight="20dp"/&gt;
  
  &lt;CheckBox
      android:id="@+id/enableVideoCheckBox"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@+id/enableAudioCheckBox"
      android:text="Enable Video"
      android:layout_marginLeft="20dp"
      android:layout_marginRight="20dp"/&gt;
  
  &lt;CheckBox
      android:id="@+id/enableMotorheckBox"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@+id/enableVideoCheckBox"
      android:text="Enable Motor"
      android:layout_marginLeft="20dp"
      android:layout_marginRight="20dp"/&gt;
  
  &lt;Button
      android:id="@+id/addSongBtn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/enableMotorheckBox"
      android:text="Add Song to Playlist"
      android:layout_marginLeft="20dp"
      android:layout_marginRight="20dp"/&gt;
  
  &lt;Button
      android:id="@+id/recordSongBtn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/addSongBtn"
      android:text="Record a song"
      android:layout_marginLeft="20dp"
      android:layout_marginRight="20dp"/&gt;
  
  &lt;Button
      android:id="@+id/playlistBtn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/recordSongBtn"
      android:text="Create a new playlist"
      android:layout_marginLeft="20dp"
      android:layout_marginRight="20dp"
      android:onClick="playlistActivity"/&gt;
  
  &lt;ListView
      android:id="@+id/playList"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/addSongBtn"
      android:layout_alignRight="@+id/addSongBtn"
      android:layout_below="@+id/playlistBtn" &gt;
  &lt;/ListView&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>