title: Why is my app crashing when I try to write to an external file?
body: <p>In this activity, I am trying to save a string to an external file in response to a button being pressed. I have been using the other posts about writing to an external file but each one I try is crashing my app. I am also unsure if my AVD is the root of the issue and has the proper settings. Can someone take a look and see if there are any errors in my AVD settings or my activity file? Thank you in advance.</p>
  
  <p>Here are my settings (not able to post images yet):</p>
  
  <p>Device: 10.1" WXGA (Tablet) (1280 x 800 mdpi)</p>
  
  <p>Target: Android 4.4.2-API Level 19</p>
  
  <p>CPU/ABI: ARM (armeabi-v7a)</p>
  
  <p>Hardware keyboard present</p>
  
  <p>Skin: Skin with dynamic hardware controls</p>
  
  <p>Front Camera: Webcam0</p>
  
  <p>Back Camera: Webcam0</p>
  
  <p>Memory Options: Ram:512   VM Heap: 32</p>
  
  <p>Internal Storage: 200 MiB</p>
  
  <p>SD Card: Size: 200 MiB</p>
  
  <p>Here is my activity's java file:</p>
  
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.PrintWriter;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.os.Environment;
  import android.support.v4.app.Fragment;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;
  
  public class FourthActivity extends Activity {
  
  private static final String TAG = "MEDIA";
  private TextView tv;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.fragment_fourth);
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
  
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.fourth, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      if (id == R.id.action_settings) {
          return true;
      }
      return super.onOptionsItemSelected(item);
  }
  
  
  
  /**
   * A placeholder fragment containing a simple view.
   */
  public static class PlaceholderFragment extends Fragment {
  
      public PlaceholderFragment() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_fourth,
                  container, false);
          return rootView;
      }
  }
  
     /** Called when the user clicks an answer */
  
  public void q1a1(){
      File root = android.os.Environment.getExternalStorageDirectory();
      tv.append("\nExternal file system root: "+root);
  
      File dir = new File (root.getAbsolutePath() + "/download");
      dir.mkdirs();
      File file = new File(dir, "myData.txt");
  
      try {
          FileOutputStream f = new FileOutputStream(file);
          PrintWriter pw = new PrintWriter(f);
          pw.println("@string/q1a1");
          pw.flush();
          pw.close();
      } catch (FileNotFoundException e) {
          e.printStackTrace();
          Log.i(TAG, "***** File not found. Did you" + "add a permission to the manifest?");
      }
      tv.append("\n\nFile written to "+file);
  
      Intent intent = new Intent(FourthActivity.this, FifthActivity.class);
      startActivity(intent);
  }
  </code></pre>
  
  <p>I believe my layout file is fine but I can add it if anyone would like to see it as well.</p>
  
  <p>Here is the error log:</p>
  
  <pre><code>06-10 10:40:44.311: E/AndroidRuntime(4857):     at android.view.View.performClick(View.java:4438)
  06-10 10:40:44.311: E/AndroidRuntime(4857):     at android.view.View$PerformClick.run(View.java:18422)
  06-10 10:40:44.311: E/AndroidRuntime(4857):     at android.os.Handler.handleCallback(Handler.java:733)
  06-10 10:40:44.311: E/AndroidRuntime(4857):     at android.os.Handler.dispatchMessage(Handler.java:95)
  06-10 10:40:44.311: E/AndroidRuntime(4857):     at android.os.Looper.loop(Looper.java:136)
  06-10 10:40:44.311: E/AndroidRuntime(4857):     at android.app.ActivityThread.main(ActivityThread.java:5017)
  06-10 10:40:44.311: E/AndroidRuntime(4857):     at java.lang.reflect.Method.invokeNative(Native Method)
  06-10 10:40:44.311: E/AndroidRuntime(4857):     at java.lang.reflect.Method.invoke(Method.java:515)
  06-10 10:40:44.311: E/AndroidRuntime(4857):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  06-10 10:40:44.311: E/AndroidRuntime(4857):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  06-10 10:40:44.311: E/AndroidRuntime(4857):     at dalvik.system.NativeStart.main(Native Method)
  06-10 10:40:44.311: E/AndroidRuntime(4857): Caused by: java.lang.reflect.InvocationTargetException
  06-10 10:40:44.311: E/AndroidRuntime(4857):     at java.lang.reflect.Method.invokeNative(Native Method)
  06-10 10:40:44.311: E/AndroidRuntime(4857):     at java.lang.reflect.Method.invoke(Method.java:515)
  06-10 10:40:44.311: E/AndroidRuntime(4857):     at android.view.View$1.onClick(View.java:3818)
  06-10 10:40:44.311: E/AndroidRuntime(4857):     ... 11 more
  06-10 10:40:44.311: E/AndroidRuntime(4857): Caused by: java.lang.NullPointerException
  06-10 10:40:44.311: E/AndroidRuntime(4857):     at com.example.motorimagery.FourthActivity.q1a1(FourthActivity.java:75)
  06-10 10:40:44.311: E/AndroidRuntime(4857):     ... 14 more
  </code></pre>
  
  <p>Here is the XML file:</p>
  
  <pre><code>    &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" &gt;
  
      &lt;TextView
      android:layout_width="736dp"
      android:layout_height="226dp"
      android:text="@string/Question1"
      android:textSize="40sp" /&gt;
  
      &lt;Button
      android:layout_width="200dp"
      android:layout_height="100dp"
      android:onClick="q1a1"
      android:text="@string/Q1A1" 
      android:gravity="center" 
      android:textSize="40sp"
      android:clickable="true"/&gt;
  </code></pre>
  
  <p></p>