0

我进入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

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

4

2 回答 2

0

我认为您的问题可能很简单,因为您的文件名似乎构造不正确:

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

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

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

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

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

您可以查看我的博客文章,了解为什么以这种方式而不是硬编码路径很重要。

最后一个建议是,在你的catch街区里,不要破坏IOException你正在捕捉的原版。如果你想添加一些上下文,那很好。但是,不仅throw new Error("Error copying database.")要调试哪个非常困难,还要尝试throw new IOException("Error copying database.", e)哪个也会传递原始异常,从而使其更容易调试。

于 2013-05-22T13:23:10.377 回答
0

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

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

只需使用这个:

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

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

希望能帮助到你

于 2013-05-22T13:29:02.050 回答