2011-12-19 177 views
1

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

点击次数和保存/导出

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

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

Information on code found here.

+0

嗨,我正好在做同样的事情,即出口我的数据库的表(S)为XML,但在同样的线路181因此得到一个NPE,我们有什么内线传球'.. = new DataXmlExporter(WHAT_IS PASSED_HERE?);'我也在Google代码中关注了您的bookworm项目,但无法找到对xml导出的引用? (刚刚发现这[pasteBin](http://pastebin.com/BLitBH5x)) – beerBear 2013-01-22 07:51:58

回答

1

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

WWActivity.this.application 
WWActivity.this.application.getDataHelper() 
+0

当它调用数据库('WWDatabaseHelper')返回一个SQLitleDatabase,没有别的('私人SQLiteDatabase ourdb' ..... ... return ourdb;'),并且'application'方法正在调用一个扩展'Application'的类,而在类内我们需要''getDataHelper''正在返回数据库。 – Christian 2011-12-20 00:18:20

相关问题