执行doInBackground()时发生错误

时间:2013-05-22 13:08:32

标签: android exception android-asynctask

我在FATAL EXCEPTION: AsyncTask #2中收到doInBackground:不要考虑String... urlActus,因为我已删除此帖子

protected Void doInBackground(String... urlActus) {         

    try {

        myDbHelper.createDataBase();

    } catch (IOException ioe) {

        throw new Error("Unable to create database");

    }
} 

public void createDataBase() throws IOException{

    boolean dbExist = checkDataBase();

    if(dbExist){
        this.getWritableDatabase();
    }
    dbExist = checkDataBase();
    if (!dbExist) {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }

}

private static String DB_NAME = "externalDB";
DB_PATH="/data/data"+context.getPackageName()+"/databases/";


private void copyDataBase() throws IOException{


    InputStream myInput = myContext.getAssets().open(DB_NAME);

    String outFileName = DB_PATH + DB_NAME;

    OutputStream myOutput = new FileOutputStream(outFileName); //EROR LINE

    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    myOutput.flush();
    myOutput.close();
    myInput.close();

}

我不明白为什么在doInBackground尝试构建期间 - > OutputStream myOutput = new FileOutputStream(outFileName);

这是我的LogCat输出:

05-22 14:51:22.078: E/AndroidRuntime(8400): FATAL EXCEPTION: AsyncTask #2
05-22 14:51:22.078: E/AndroidRuntime(8400): java.lang.RuntimeException: An error occured while executing doInBackground()
05-22 14:51:22.078: E/AndroidRuntime(8400):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
05-22 14:51:22.078: E/AndroidRuntime(8400):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-22 14:51:22.078: E/AndroidRuntime(8400):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-22 14:51:22.078: E/AndroidRuntime(8400):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-22 14:51:22.078: E/AndroidRuntime(8400):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-22 14:51:22.078: E/AndroidRuntime(8400):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-22 14:51:22.078: E/AndroidRuntime(8400):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-22 14:51:22.078: E/AndroidRuntime(8400):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-22 14:51:22.078: E/AndroidRuntime(8400):     at java.lang.Thread.run(Thread.java:856)
05-22 14:51:22.078: E/AndroidRuntime(8400): Caused by: java.lang.Error: Error copying database
05-22 14:51:22.078: E/AndroidRuntime(8400):     at com.example.savingfiles.DatabaseHelper.createDataBase(DatabaseHelper.java:67)
05-22 14:51:22.078: E/AndroidRuntime(8400):     at com.example.savingfiles.SplashActivity$Traitement.doInBackground(SplashActivity.java:81)
05-22 14:51:22.078: E/AndroidRuntime(8400):     at com.example.savingfiles.SplashActivity$Traitement.doInBackground(SplashActivity.java:1)
05-22 14:51:22.078: E/AndroidRuntime(8400):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-22 14:51:22.078: E/AndroidRuntime(8400):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-22 14:51:22.078: E/AndroidRuntime(8400):     ... 5 more

我有这个例外,我可以采取什么方法来避免这种情况?

2 个答案:

答案 0 :(得分:0)

我想也许您的问题可能就像您的文件名似乎构造错误一样简单:

DB_PATH="/data/data"+context.getPackageName()+"/databases/";

您似乎缺少“数据”和包名称之间的斜杠:

DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";

但实际上,更好的一步就是不要对路径进行硬编码。相反,使用这个:

DB_PATH = context.getFilesDir().getAbsolutePath() + "/databases/";

您可以查看my blog post以这种方式执行此操作的重要性,而不是硬编码路径。

最后一个建议是,在catch区块中,不要破坏您正在捕捉的原始IOException。如果你想添加一些上下文,那很好。但是,而不仅仅是throw new Error("Error copying database.")这很难调试,请尝试throw new IOException("Error copying database.", e),它也会传递原始异常,使调试变得更加容易。

答案 1 :(得分:0)

尝试查看outFileName(记录或调试)的值,您会发现它不正确。

另一个推荐是不隐藏原始异常。您正在捕获IOException,其中包含有关错误的真实信息,然后您没有将它传递给Error类,因此您丢失了有关失败的真正原因的任何信息(FileNotFound,权限问题......)

请使用:

       try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database",e);
        }

并将原始异常记录为嵌套异常,以便您查看实际问题。

希望有所帮助