Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid in Eclipse - AsyncTask upon Button click initiates FileWriter
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/users/217283/eric-nordvik">Eric Nordvik</a> has been helping me with this <a href="https://stackoverflow.com/questions/9529210/android-in-eclipse-invoke-file-writer-class-in-background">issue</a> for some time now so big props to him.</p> <p>This question has sort of evolved into an AsyncTask question so I figured I'd move it here. When my print button (btnPrintTardy) is click I was to start an AsyncTask to save whatever is in the edit text field (editText1) via FileWriter to the SD card as a .text .</p> <p>So far here is my AsyncTask called "FileWriterTask"</p> <pre><code>package com.android.upgrayeddapps.latepass; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import android.content.Context; import android.os.AsyncTask; import android.view.View; import android.widget.Toast; public class FileWriterTask extends AsyncTask&lt;String, Void, Void&gt; { @Override protected Void doInBackground(String... params) { // write in data folder try { File myFile = new File("/sdcard/StudentLatePass.txt"); myFile.createNewFile(); FileOutputStream fOut = new FileOutputStream(myFile); OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); myOutWriter.append(params[0]); myOutWriter.close(); fOut.close(); Toast.makeText(getBaseContext(), "Finished writing StudentLatePass.txt'", Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); } finish(); return null; } private Context getBaseContext() { // TODO Auto-generated method stub return null; } private void finish() { // TODO Auto-generated method stub }; public void onClick(View v) { // clear text box finish(); } } </code></pre> <p>Inside my Student Activity I am calling my FileWriterTask with this code.</p> <pre><code>public void UpdateStudenttxtfile(View View) { EditText txtData = (EditText) findViewById(R.id.editText1); FileWriterTask task = new FileWriterTask(); task.execute(txtData.getText().toString()); } </code></pre> <p>In my studentActivity layout I have the "printbutton's" on Click set to</p> <pre><code>android:onClick="UpdateStudenttxtfile" </code></pre> <p>Now at the moment when I run this I am getting a few exceptions and the program crashes.</p> <pre><code>I/dalvikvm(337): Jit: resizing JitTable from 512 to 1024 W/dalvikvm(337): threadid=9: thread exiting with uncaught exception (group=0x40015560) E/AndroidRuntime(337): FATAL EXCEPTION: AsyncTask #1 E/AndroidRuntime(337): java.lang.RuntimeException: An error occured while executing doInBackground() E/AndroidRuntime(337): at android.os.AsyncTask$3.done(AsyncTask.java:200) E/AndroidRuntime(337): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) E/AndroidRuntime(337): at java.util.concurrent.FutureTask.setException(FutureTask.java:125) E/AndroidRuntime(337): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) E/AndroidRuntime(337): at java.util.concurrent.FutureTask.run(FutureTask.java:138) E/AndroidRuntime(337): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) E/AndroidRuntime(337): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) E/AndroidRuntime(337): at java.lang.Thread.run(Thread.java:1019) E/AndroidRuntime(337): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() E/AndroidRuntime(337): at android.os.Handler.&lt;init&gt;(Handler.java:121) E/AndroidRuntime(337): at android.widget.Toast.&lt;init&gt;(Toast.java:68) E/AndroidRuntime(337): at android.widget.Toast.makeText(Toast.java:231) E/AndroidRuntime(337): at com.android.upgrayeddapps.latepass.FileWriterTask.doInBackground(FileWriterTask.java:29) E/AndroidRuntime(337): at com.android.upgrayeddapps.latepass.FileWriterTask.doInBackground(FileWriterTask.java:1) E/AndroidRuntime(337): at android.os.AsyncTask$2.call(AsyncTask.java:185) E/AndroidRuntime(337): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) E/AndroidRuntime(337): ... 4 more D/dalvikvm(337): GC_CONCURRENT freed 1381K, 60% free 2709K/6727K, external 2011K/2137K, paused 7ms+8ms I/Process(337): Sending signal. PID: 337 SIG: 9 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload