2013-04-06 47 views
1

我有一個在Google Nexus(Android 4.2)中成功運行的代碼。但是當我在android 2.3.5的較低版本中嘗試它時,它會拋出一個異常。AsyncTask在Android 2.3中不引發異常Andengine

請幫我解決同樣的問題。

詳情。

我創建了一個類DownloadHelper,它可以幫助我從互聯網下載文件到手機中的某個位置。 本課實習生致電。當我嘗試創建一個對象DownloadFile時,它會拋出異常。

下面是拋出異常的的DownloadHeper

import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.URL; 
import java.net.URLConnection; 

import org.andengine.entity.text.Text; 
import org.andengine.ui.activity.BaseGameActivity; 

import com.gretrainer.gretrainer.StartingScreen; 

import android.os.AsyncTask; 
import android.os.Environment; 
import android.util.Log; 

public class DownloadHelper { 
    public void loadFile(String url,Text loadingT,BaseGameActivity activity1,String _filename,int _tag){ 
     DownloadFile downloadFile; 
     try { 
      downloadFile = new DownloadFile(); 
      loadingText = loadingT; 
      activity = activity1; 
      downloadFile.execute(url); 
      StartingPercent = 0; 
      EndingPercent = 100; 
      filename = _filename; 
      tag = _tag; 
     } catch (Exception e) { 
      Log.d("exec",e.getLocalizedMessage()); 
     } 



    } 

    private int tag; 
    private String filename; 
    private float StartingPercent; 
    private float EndingPercent; 
    private BaseGameActivity activity; 
    private Text loadingText; 
    private class DownloadFile extends AsyncTask<String,Integer,String>{ 

     public DownloadFile(){ 

     } 

     @Override 
     protected String doInBackground(String... sUrl) { 
      try { 
       URL url = new URL(sUrl[0]); 
       URLConnection connection = url.openConnection(); 
       connection.connect(); 
       // this will be useful so that you can show a typical 0-100% progress bar 
       int fileLength = connection.getContentLength(); 

       // download the file 
       InputStream input = new BufferedInputStream(url.openStream()); 
       OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().toString().concat(File.separator + filename)); 


       byte data[] = new byte[1024]; 
       long total = 0; 
       int count; 
       while ((count = input.read(data)) != -1) { 
        total += count; 
        // publishing the progress.... 
        publishProgress((int) (total * 100/fileLength)); 
        output.write(data, 0, count); 
       } 

       output.flush(); 
       output.close(); 
       input.close(); 
       StartingScreen act = (StartingScreen)activity; 
       act.onLoadFileComplete(tag); 
      } catch (Exception e) { 
       String message = e.getLocalizedMessage(); 
       Log.d("hello",message); 
      } 
      return null; 
     } 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
      } 

      @Override 
      protected void onProgressUpdate(Integer... progress) { 
       super.onProgressUpdate(progress); 
       activity.runOnUiThread(new Runnable(){ 

        @Override 
        public void run() { 
         // TODO Auto-generated method stub 

        } 

       }); 
       //loadingText.setText(StartingPercent + ((EndingPercent - StartingPercent)/100) * progress[0] + "%"); 
      } 


    } 
} 

代碼時它執行線downloadFile =新DownloadFile();

Exception的細節如下所示。

UpdateThread interrupted. Don't worry - this EngineDestroyedException is most likely expected! 
org.andengine.engine.Engine$EngineDestroyedException 

它回到最後一行onCreateScene。但該應用程序已凍結。 它工作在新的Android操作系統(4.2)的罰款

請從異常 的時間檢查

整個日誌04-07 17:46:42.498:W/dalvikvm(2552):異常Ljava /朗/ RuntimeException的;在初始化Landroid/os/AsyncTask時拋出; 04-07 17:47:06.891:E/AndEngine(2552):StartingScreen.onPopulateScene失敗。 @(Thread:'GLThread 11') 04-07 17:47:06.891:E/AndEngine(2552):java.lang.ExceptionInInitializerError 04-07 17:47:06.891:E/AndEngine(2552):at com .gretrainer.gretrainer.AppHelpers.DownloadHelper.loadFile(DownloadHelper.java:24) 04-07 17:47:06.891:E/AndEngine(2552):at com.gretrainer.gretrainer.StartingScreen.onPopulateScene(StartingScreen.java:102 ) 04-07 17:47:06.891:E/AndEngine(2552):at org.andengine.ui.activity.BaseGameActivity $ 2.onCreateSceneFinished(BaseGameActivity.java:154) 04-07 17:47:06.891:E/AndEngine(2552):at com.gretrainer.gretrainer.StartingScreen.onCreateScene(StartingScreen.java:91) 04-07 17:47:06.891:E/AndEngine(2552):at org.andengine.ui.activity.BaseGameActivity $ 3 .onCreateResourcesFinished(BaseGameActivity.java:169) 04-07 17:47:06.891:E/AndE ngine(2552):at com.gretrainer.gretrainer.StartingScreen.onCreateResources(StartingScreen.java:68) 04-07 17:47:06.891:E/AndEngine(2552):at org.andengine.ui.activity.BaseGameActivity。 onCreateGame(BaseGameActivity.java:181) 04-07 17:47:06.891:E/AndEngine(2552):at org.andengine.ui.activity.BaseGameActivity.onSurfaceCreated(BaseGameActivity.java:110) 04-07 17: 47:06.891:E/AndEngine(2552):在org.andengine.opengl.view.EngineRenderer.onSurfaceCreated(EngineRenderer.java:80) 04-07 17:47:06.891:E/AndEngine(2552):at android。 opengl.GLSurfaceView $ GLThread.guardedRun(GLSurfaceView.java:1352) 04-07 17:47:06.891:E/AndEngine(2552):at android.opengl.GLSurfaceView $ GLThread.run(GLSurfaceView.java:1122) 04 -07 17:47:06.891:E/AndEngine(2552):引起:java.lang.RuntimeException:無法在裏面創建處理程序線程沒有調用Looper.prepare() 04-07 17:47:06.891:E/AndEngine(2552):at android.os.Handler。(Handler.java:121) 04-07 17:47:06.891 :E/AndEngine(2552):at android.os.AsyncTask $ InternalHandler。(AsyncTask.java:421) 04-07 17:47:06.891:E/AndEngine(2552):at android.os.AsyncTask $ InternalHandler。 (的AsyncTask。java:421) 04-07 17:47:06.891:E/AndEngine(2552):at android.os.AsyncTask。(AsyncTask.java:152) 04-07 17:47:06.891:E/AndEngine(2552 ):... 11更多 04-07 17:47:06.891:D/AndEngine(2552):StartingScreen.onSurfaceChanged(Width = 320,Height = 480)@(Thread:'GLThread 11') 04-07 17 :47:07.102:D/dalvikvm(2552):GC_FOR_MALLOC釋放142K,42%空閒3211K/5447K,外部1K/512K,暫停26ms 04-07 17:47:07.102:I/dalvikvm-heap堆棧(碎片大小)爲6.457MB,用於1382416字節的分配 04-07 17:47:07.222:D/dalvikvm(2552):GC_CONCURRENT釋放2K,34%空閒4559K/6855K,外部1K/512K,暫停3ms + 4ms 04-07 17:47:07.222:D/AndEngine(2552):StartingScreen.onResumeGame @(Thread:'main') 04-07 17:47:07.292:E/Database(2552):close前顯式調用數據庫'/data/data/com.gretrainer.gretrainer/databases/GreApp' 04-07 17:47:07.292:E/Database(2552):android.database.sqlite.DatabaseObjectNotClosedException:應用程序未關閉遊標或數據庫對象在此處打開 04-07 17:47:07.292:E/Database(2552):at android.database.sqlite.SQLiteDatabase。(SQLiteDatabase.java:1960) 04-07 17:47:07.292 :E/Database(2552):at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:906) 04-07 17:47:07.292:E/Database(2552):at android.database.sqlite.SQLiteDatabase .openOrCreateDatabase(SQLiteDatabase.java:940) 04-07 17:47:07.292:E/Database(2552):at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:933) 04-07 17:47 :07.292:E /數據庫(2552):在android.app.ContextImpl.openOrCre ateDatabase(ContextImpl.java:744) 04-07 17:47:07.292:E/Database(2552):at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203) 04-07 17:47:07.292: E/Database(2552):at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118) 04-07 17:47:07.292:E/Database(2552):at android.database.sqlite.SQLiteOpenHelper。 getReadableDatabase(SQLiteOpenHelper.java:198) 04-07 17:47:07.292:E/Database(2552):at com.gretrainer.gretrainer.AppHelpers.DatabaseHandler.getWordsCount(DatabaseHandler.java:213) 04-07 17: 47:07.292:E/Database(2552):at com.gretrainer.gretrainer.StartingScreen.onPopulateScene(StartingScreen.java:100) 04-07 17:47:07.292:E/Database(2552):at org.andengine。 ui.activity.BaseGameActivity $ 2.onCreateSceneFinished(BaseGameActivity.java:154)(2552):at com.gretrainer.gretrainer.StartingScreen.onCreateScene(StartingScreen.java:91) 04-07 17:47:07.292:E/Database(2552) :at org.andengine.ui.activity.BaseGameActivity $ 3.onCreateResourcesFinished(BaseGameActivity.java:169) 04-07 17:47:07.292:E/Database(2552):at com.gretrainer.gretrainer.StartingScreen.onCreateResources(StartingScreen .java:68) 04-07 17:47:07.292:E/Database(2552):at org.andengine.ui.activity.BaseGameActivity.onCreateGame(BaseGameActivity.java:181) 04-07 17:47:07.292 :E/Database(2552):at org.andengine.ui.activity.BaseGameActivity.onSurfaceCreated(BaseGameActivity.java:110) 04-07 17:47:07.292:E/Database(2552):at org.andengine.opengl .view.EngineRenderer.onSurfaceCreated(EngineRenderer.java:80) 04-07 17:47:07.292:E/Da tabase(2552):at android.opengl.GLSurfaceView $ GLThread.guardedRun(GLSurfaceView.java:1352) 04-07 17:47:07.292:E/Database(2552):at android.opengl.GLSurfaceView $ GLThread.run GLSurfaceView.java:1122)

+3

添加整個日誌...當您可以看到整個堆棧跟蹤時,它更容易... – 2013-04-06 03:22:24

+0

我添加了整個日誌。請檢查@StevenByle – 2013-04-07 12:20:15

回答

4

這只是一個猜測,但有幾個Exceptions會導致您未正確執行AsyncTask的結論(請參閱Can't create handler inside thread that has not called Looper.prepare()。我不是建議,自己打電話Looper.prepare(),但在另一個線程上執行Asynctask。因此,而不是

downloadFile = new DownloadFile(); 
loadingText = loadingT; 
activity = activity1; 
downloadFile.execute(url); 
StartingPercent = 0; 
EndingPercent = 100; 
filename = _filename; 
tag = _tag; 

嘗試這樣的事情(順便說一句,你正在訪問你的任務中的某些變量,應該更早初始化):

loadingText = loadingT; 
activity = activity1; 
StartingPercent = 0; 
EndingPercent = 100; 
filename = _filename; 
tag = _tag; 
runOnUiThread(new Runnable() { 
    @Override 
    public void run() { 
     new DownloadFile().execute(url); 
    } 
}); 

我與AsyncTask的經驗是,他們不應該因爲此線程僅用於刷新屏幕,所以請嘗試在UIThread上運行它(因爲您不是直接在任務中顯示數據),因此不會在正常的Update Thread上運行。

+0

這樣做的伎倆。謝謝 – 2013-04-09 01:42:57