我正在使用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();
}
}
}
登录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());
请帮助我一直在寻找几个小时的答案。谢谢。
答案 0 :(得分:1)
显然有些东西是null
,你应该弄清楚是什么/为什么。这是其中之一:
WWActivity.this.application
WWActivity.this.application.getDataHelper()