1
votes

I have a code which successfully runs in Google Nexus(Android 4.2). But when I tried it yesterday in a lower version with android 2.3.5 it throws an Exception.

Please help me resolve the same.

Details.

I created a class DownloadHelper which help me download a file from internet to a location in my phone. This class intern calls a sub class DownloadFile extends AsyncTask. When I try to create a object of DownloadFile it throws the exception.

Below is the code of 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] + "%");
            }


    }
}

The exception is thrown when it executes the line downloadFile = new DownloadFile();

The details of Exception is as shown below.

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

It goes back to last line of onCreateScene. but the app is freezed. It works fine in new android os(4.2)

Please check

Entire log from the time of exception 04-07 17:46:42.498: W/dalvikvm(2552): Exception Ljava/lang/RuntimeException; thrown while initializing Landroid/os/AsyncTask; 04-07 17:47:06.891: E/AndEngine(2552): StartingScreen.onPopulateScene failed. @(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/AndEngine(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): at 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): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called 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 more 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 freed 142K, 42% free 3211K/5447K, external 1K/512K, paused 26ms 04-07 17:47:07.102: I/dalvikvm-heap(2552): Grow heap (frag case) to 6.457MB for 1382416-byte allocation 04-07 17:47:07.222: D/dalvikvm(2552): GC_CONCURRENT freed 2K, 34% free 4559K/6855K, external 1K/512K, paused 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() was never explicitly called on database '/data/data/com.gretrainer.gretrainer/databases/GreApp' 04-07 17:47:07.292: E/Database(2552): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here 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/Database(2552): at android.app.ContextImpl.openOrCreateDatabase(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) 04-07 17:47:07.292: E/Database(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/Database(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)

1
Add the whole log... it's much easier when you can see the entire stack trace...Steven Byle
I have added the whole log. Please check @StevenByleGeo Paul

1 Answers

4
votes

This is only a guess but several of the Exceptions lead to the conclusion that you are not executing the AsyncTask correctly (see Can't create handler inside thread that has not called Looper.prepare(). I am not suggesting, calling Looper.prepare() by yourself, but executing the Asynctask on another thread. So instead of

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

try something like this (btw, you are accessing some variables within your task, that should be initialized earlier):

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

My experience with AsyncTasks is, that they shouldn't run on the normal Update Thread, since this thread is for refreshing the screen only, so try running it on the UIThread instead (Since you are not displaying data directly from within the task).