执行 doInBackground() 时发生错误

发布于 2024-12-21 21:35:21 字数 6885 浏览 0 评论 0原文

我正在使用 doInBackground() 方法将数据库保存为 xml 文件并备份数据库。它备份数据库并保存它,但在尝试保存到 xml 文件时强制关闭。

点击并保存/导出

@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());

请帮助我几个小时以来一直在寻找答案。谢谢。

有关此处找到的代码的信息。

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.

ON CLICK AND SAVE/EXPORT

@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

LINE 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.

Information on code found here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好久不见√ 2024-12-28 21:35:21

显然有些东西是null,你应该弄清楚是什么/为什么。这是其中之一:

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

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

WWActivity.this.application
WWActivity.this.application.getDataHelper()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文