在执行doInBackground时出错() [英] An error occured while executing doInBackground()

查看:324
本文介绍了在执行doInBackground时出错()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是doInBackground()方法,我的数据库保存为XML文件,并备份数据库。它备份数据库并保存它,但试图保存到XML文件时,强制关闭。

I am using a doInBackground() method to save my database as a xml file and back up the database. it backs up the database and saves it but force closes when trying to save into xml file.

点击次数和保存/导出

@Override
public void onClick(View v) {
    case R.id.clockOut:

        /*if (WWActivity.this.isExternalStorageAvail()) {
               new ExportDatabaseFileTask().execute();
            } else {
               Toast.makeText(WWActivity.this, "External storage is not available, unable to export data.",
                        Toast.LENGTH_SHORT).show();
            }*/

         if (WWActivity.this.isExternalStorageAvail()) {
               new ExportDataAsXmlTask().execute("exampledb", "exampledata");
            } else {
               Toast.makeText(WWActivity.this, "External storage is not available, unable to export data.",
                        Toast.LENGTH_SHORT).show();
            }



        timer.open();
        timer.saveTime(mName, mLat, mLon, mName, mName, mName, mName, mName);
        timer.close();

        /*DatabaseAssistants DA = new DatabaseAssistants(myContext, mySQLiteDatabase);
        DA.exportData();*/

        Intent stop = new Intent (WWActivity.this, SetTime.class);
        stopService(stop);
        break;
    }



}

private boolean isExternalStorageAvail() {
    // TODO Auto-generated method stub
    return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}

private class ExportDataAsXmlTask extends AsyncTask<String, Void, String> {
      private final ProgressDialog dialog = new ProgressDialog(WWActivity.this);

      // can use UI thread here
      protected void onPreExecute() {
         this.dialog.setMessage("Exporting database as XML...");

      }

      // automatically done on worker thread (separate from UI thread)
      protected String doInBackground(final String... args) {
         DataXmlExporter dm = new DataXmlExporter(WWActivity.this.application.getDataHelper().WWDatabaseHelper());
         try {
            String dbName = args[0];
            String exportFileName = args[1];
            dm.export(dbName, exportFileName);
         } catch (IOException e) {
            Log.e(MyApplication.APP_NAME, e.getMessage(), e);
            return e.getMessage();
         }
         return null;
      }

      // can use UI thread here
      protected void onPostExecute(final String errMsg) {
         if (this.dialog.isShowing()) {
            this.dialog.dismiss();
         }
         if (errMsg == null) {
            Toast.makeText(WWActivity.this, "Export successful!", Toast.LENGTH_SHORT).show();
         } else {
            Toast.makeText(WWActivity.this, "Export failed - " + errMsg, Toast.LENGTH_SHORT).show();
         }
      }
   }

 private class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
      private final ProgressDialog dialog = new ProgressDialog(WWActivity.this);

      // can use UI thread here
      protected void onPreExecute() {
         this.dialog.setMessage("Exporting database...");
         this.dialog.show();
      }

      // automatically done on worker thread (separate from UI thread)
      protected Boolean doInBackground(final String... args) {

         File dbFile =
                  new File(Environment.getDataDirectory() + "/data/com.totsp.androidexamples/databases/example");

         File exportDir = new File(Environment.getExternalStorageDirectory(), "exampledata");
         if (!exportDir.exists()) {
            exportDir.mkdirs();
         }
         File file = new File(exportDir, dbFile.getName());

         try {
            file.createNewFile();
            this.copyFile(dbFile, file);
            return true;
         } catch (IOException e) {
            Log.e(MyApplication.APP_NAME, e.getMessage(), e);
            return false;
         }
      }

      // can use UI thread here
      protected void onPostExecute(final Boolean success) {
         if (this.dialog.isShowing()) {
            this.dialog.dismiss();
         }
         if (success) {
            Toast.makeText(WWActivity.this, "Export successful!", Toast.LENGTH_SHORT).show();
         } else {
            Toast.makeText(WWActivity.this, "Export failed", Toast.LENGTH_SHORT).show();
         }
      }

      void copyFile(File src, File dst) throws IOException {
         FileChannel inChannel = new FileInputStream(src).getChannel();
         FileChannel outChannel = new FileOutputStream(dst).getChannel();
         try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
         } finally {
            if (inChannel != null)
               inChannel.close();
            if (outChannel != null)
               outChannel.close();
         }
      }

   }

LOG CAT

12-19 13:02:23.202: E/AndroidRuntime(1980): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
12-19 13:02:23.202: E/AndroidRuntime(1980): java.lang.RuntimeException: An error occured while executing doInBackground()
12-19 13:02:23.202: E/AndroidRuntime(1980):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-19 13:02:23.202: E/AndroidRuntime(1980):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-19 13:02:23.202: E/AndroidRuntime(1980):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-19 13:02:23.202: E/AndroidRuntime(1980):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-19 13:02:23.202: E/AndroidRuntime(1980):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-19 13:02:23.202: E/AndroidRuntime(1980):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-19 13:02:23.202: E/AndroidRuntime(1980):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
12-19 13:02:23.202: E/AndroidRuntime(1980):     at java.lang.Thread.run(Thread.java:1096)
 12-19 13:02:23.202: E/AndroidRuntime(1980): Caused by: java.lang.NullPointerException
 12-19 13:02:23.202: E/AndroidRuntime(1980):    at www.freshapp.com.wherewhen.html.WWActivity$ExportDataAsXmlTask.doInBackground(WWActivity.java:181)
12-19 13:02:23.202: E/AndroidRuntime(1980):     at www.freshapp.com.wherewhen.html.WWActivity$ExportDataAsXmlTask.doInBackground(WWActivity.java:1)
12-19 13:02:23.202: E/AndroidRuntime(1980):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-19 13:02:23.202: E/AndroidRuntime(1980):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-19 13:02:23.202: E/AndroidRuntime(1980):     ... 4 more

订单181

             DataXmlExporter dm = new DataXmlExporter(WWActivity.this.application.getDataHelper().WWDatabaseHelper());

请帮我一直在寻找了几个小时一个答案了。谢谢你。

Please help I have been looking for an answer for a few hours now. thank you.

上code的信息在这里找到。

推荐答案

显然,事情是,你应该弄清楚什么/为什么。这是其中的一个:

Clearly something is null and you should figure out what/why. It's one of these:

WWActivity.this.application
WWActivity.this.application.getDataHelper()

这篇关于在执行doInBackground时出错()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆