Android 尝试在Asynctask中设置水平进度条会引发NullPointerException
我正在尝试使用Android 尝试在Asynctask中设置水平进度条会引发NullPointerException,android,progress-bar,android-asynctask,Android,Progress Bar,Android Asynctask,我正在尝试使用AsyncTask将CSV文件导出到SD卡,并使用ProgressBar告诉用户该任务是如何运行的。然而,当我试图用setProgress设置进度条时,我得到一个空指针异常。我的Logcat将我传递的整数显示为8,因此它不是null。如果我注释掉设置进度条的尝试,它将正确运行。当我尝试设置进度条时,运行时看到的是null吗 完整的异步任务实现: public class Exporter extends AsyncTask<File, Integer, Boolean>
AsyncTask
将CSV文件导出到SD卡,并使用ProgressBar
告诉用户该任务是如何运行的。然而,当我试图用setProgress
设置进度条时,我得到一个空指针异常。我的Logcat将我传递的整数显示为8,因此它不是null。如果我注释掉设置进度条的尝试,它将正确运行。当我尝试设置进度条时,运行时看到的是null吗
完整的异步任务实现:
public class Exporter extends AsyncTask<File, Integer, Boolean> {
private Context ctx;
private final static String TAG = "Exporter";
private ProgressDialog progress;
private GeoDatabase db;
private RecordListActivity parent;
public Exporter(Context ctx) {
super();
Log.d(TAG, "Building " + this + " from " + ctx);
this.ctx = ctx;
parent = (RecordListActivity) ctx;
db = (GeoDatabase) ctx.getApplicationContext();
}
@Override
protected void onPreExecute() {
// Initialize the progress dialog
Log.i(TAG, "Pre-executing exporter");
final String title = "Exporting";
final String msg = "Initializing export...";
progress = ProgressDialog.show(ctx, title, msg, true);
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
super.onPreExecute();
}
/**
* Begins operation of the exporter and determines if it was a
* success.
*
* @param args the destination file
*
* @return whether the transfer was successful
*/
@Override
protected Boolean doInBackground(final File... args) {
Log.d(TAG, "src-> " + "N/A" + ", dest-> " + args[0]);
// Get the ArrayList of records inside the current folder
ArrayList<Record> records = db.getRecordsIn(parent.getCurrentFolder());
FileWriter writer;
try {
// Build a FileWriter targeting the destination
writer = new FileWriter(args[0]);
// Iterate through the records to write their CSV string out
final int total = records.size();
int current = 1;
for (Record record : records) {
Log.v(TAG, "Accessing " + record);
writer.write(record.getCSVString() + "\n");
Log.v(TAG, record.getCSVString());
// Update the progress bar
onProgressUpdate(total, current);
current++;
}
// Close the writer
writer.close();
Log.d(TAG, "Writer closed");
} catch (IOException e) {
// Something has gone horribly wrong
e.printStackTrace();
Log.e(TAG, "Writing to file failed!");
return false;
}
return true;
}
@Override
protected void onProgressUpdate(Integer... values) {
// values[1] is the current record being written
// values[0] is the total number of records being written
final int total = values[0];
final int current = values[1];
// Updates the text for the current record
final String msg = "Writing record " + current + " of " + total;
progress.setMessage(msg);
Log.d(TAG, msg);
// Updates the progress bar
final int position = 100 * current / total;
Log.d(TAG, "Setting position to " + position);
progress.setProgress(position);
}
// can use UI thread here
protected void onPostExecute(final Boolean success) {
// TODO: Alert the user to success or failure - the dialog drops
// to quickly now
if (success) {
progress.setMessage("Export successful");
Log.i(TAG, "Export successful");
} else {
progress.setMessage("Export failed");
Log.e(TAG, "Export failed");
}
progress.dismiss();
}
}
找到了问题和解决方法。在Android API中,使用
ProgressDialog.show(params)
创建ProgressDialog
的工厂方法没有正确设置更新处理程序,这似乎是一个错误。因此,修订后的工作守则如下:
@Override
protected void onPreExecute() {
// Initialize the progress dialog
Log.i(TAG, "Pre-executing exporter");
final String title = "Exporting";
final String msg = "Initializing export...";
progress = new ProgressDialog(ctx);
progress.setTitle(title);
progress.setMessage(msg);
progress.setIndeterminate(false);
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
super.onPreExecute();
}
找到了问题和解决方法。在Android API中,使用
ProgressDialog.show(params)
创建ProgressDialog
的工厂方法没有正确设置更新处理程序,这似乎是一个错误。因此,修订后的工作守则如下:
@Override
protected void onPreExecute() {
// Initialize the progress dialog
Log.i(TAG, "Pre-executing exporter");
final String title = "Exporting";
final String msg = "Initializing export...";
progress = new ProgressDialog(ctx);
progress.setTitle(title);
progress.setMessage(msg);
progress.setIndeterminate(false);
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
super.onPreExecute();
}
您能否发布完整日志以及完整的
AsyncTask
,包括doInBackround
()?@LuxuryMode Done。没有太多额外的内容,但希望能有所帮助。您能发布完整的日志以及完整的AsyncTask
,包括doInBackround
()?@LuxuryMode Done。没有太多额外的,但希望它有帮助。