2012-03-15 37 views
0

Eric Nordvik一直幫我這個issue一段時間現在這麼大的道具給他。安卓在Eclipse - AsyncTask按鈕點擊啓動FileWriter

這個問題已經演變成一個AsyncTask問題,所以我想我會把它移到這裏。當我的打印按鈕(btnPrintTardy)是單擊時,我將啓動一個AsyncTask,以通過FileWriter將編輯文本字段(editText1)中的所有內容保存爲.text文件的SD卡。

到目前爲止,這是我的AsyncTask稱爲「FileWriterTask」

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<String, Void, Void> { 
     @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(); 
    } 
     } 

在我的學生活動,我打電話給我的FileWriterTask與此代碼。

public void UpdateStudenttxtfile(View View) 
    { 
     EditText txtData = (EditText) findViewById(R.id.editText1); 
     FileWriterTask task = new FileWriterTask(); 
     task.execute(txtData.getText().toString()); 

    } 

在我studentActivity佈局我有「printbutton的」上單擊設置

android:onClick="UpdateStudenttxtfile" 

現在的時刻,當我運行此我得到一些例外,程序崩潰。

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.<init>(Handler.java:121) 

E/AndroidRuntime(337): at android.widget.Toast.<init>(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 
+0

你真的需要一個AsyncTask來做到這一點嗎?我的意思是 - 有人會在EditText中寫入多少東西? – hovanessyan 2012-03-15 18:06:04

+0

最終我們將從數據庫中提供數據。即:學生名稱,時間,週期,遲到計數,警告等。由於我是Android新手,我不積極處理這個問題。 – UPGRAYEDD 2012-03-15 18:21:11

回答

1

在Android中,您不能從UI線程以外的任何線程更新UI。 在你的代碼中,你正在顯示一個在獨立線程中執行的doInBackground() Toast。