我有一个ASyncTask在后台线程上处理多个ProgressBars。 下面的行导致错误/崩溃。
@Override
protected void onPreExecute() {
progress = maskBars[currentProgressBarId].getProgress();
}
我很困难,因为它在演示中工作正常(在2个占位符片段内)。 我的片段是使用fragmentManager创建的,这可能是什么原因?
aSyncTask方法
private AsyncTaskUpdateProgress asyncTaskUpdateProgress;
public class AsyncTaskUpdateProgress extends AsyncTask<Void, Integer, Void> {
int progress;
@Override
protected void onPostExecute(Void result) {
currentProgressBarId++;
if ((maxProgressValue - currentProgressBarId * 100 > 0) && (currentProgressBarId < maskBars.length)) {
if (!isCancelled()) {
SystemClock.sleep(PROGRESS_BAR_DELAY);
asyncTaskUpdateProgress = new AsyncTaskUpdateProgress();
asyncTaskUpdateProgress.execute();
}
} else {
buttonStop.setEnabled(false);
buttonReset.setEnabled(true);
}
}
@Override
protected void onPreExecute() {
progress = maskBars[currentProgressBarId].getProgress();
}
@Override
protected void onProgressUpdate(Integer... values) {
textViewProgress.setText(String.valueOf(crgBrId * 100 + values[0]));
maskBars[currentProgressBarId].setProgress(values[0]);
}
@Override
protected Void doInBackground(Void... args) {
int limit = maxProgressValue - currentProgressBarId * 100;
if (limit > 100) { limit = 100; }
while ((!isCancelled()) && (progress < limit)) {
progress++;
publishProgress(progress);
SystemClock.sleep(PROGRESS_DELAY);
}
return null;
}
}
12-15 17:04:42.088: E/AndroidRuntime(7305): FATAL EXCEPTION: main
12-15 17:04:42.088: E/AndroidRuntime(7305): Process: com.black.app, PID: 7305
12-15 17:04:42.088: E/AndroidRuntime(7305): java.lang.NullPointerException
12-15 17:04:42.088: E/AndroidRuntime(7305): at com.myob.app.VerticalProgressBars$AsyncTaskUpdateProgress.onPreExecute(VerticalProgressBars.java:56)
12-15 17:04:42.088: E/AndroidRuntime(7305): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
12-15 17:04:42.088: E/AndroidRuntime(7305): at android.os.AsyncTask.execute(AsyncTask.java:535)
12-15 17:04:42.088: E/AndroidRuntime(7305): at com.black.app.VerticalProgressBars.onClick(VerticalProgressBars.java:140)
12-15 17:04:42.088: E/AndroidRuntime(7305): at android.view.View.performClick(View.java:4445)
12-15 17:04:42.088: E/AndroidRuntime(7305): at android.view.View$PerformClick.run(View.java:18429)
12-15 17:04:42.088: E/AndroidRuntime(7305): at android.os.Handler.handleCallback(Handler.java:733)
12-15 17:04:42.088: E/AndroidRuntime(7305): at android.os.Handler.dispatchMessage(Handler.java:95)
12-15 17:04:42.088: E/AndroidRuntime(7305): at android.os.Looper.loop(Looper.java:136)
12-15 17:04:42.088: E/AndroidRuntime(7305): at android.app.ActivityThread.main(ActivityThread.java:5089)
12-15 17:04:42.088: E/AndroidRuntime(7305): at java.lang.reflect.Method.invokeNative(Native Method)
12-15 17:04:42.088: E/AndroidRuntime(7305): at java.lang.reflect.Method.invoke(Method.java:515)
12-15 17:04:42.088: E/AndroidRuntime(7305): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
12-15 17:04:42.088: E/AndroidRuntime(7305): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
12-15 17:04:42.088: E/AndroidRuntime(7305): at dalvik.system.NativeStart.main(Native Method)
12-15 17:04:42.095: W/ActivityManager(434): Force finishing activity com.blagmynames.app/.NameActivity
12-15 17:04:42.627: W/ActivityManager(434): Activity pause timeout for ActivityRecord{41b08cf8 u0 com.blackmagicnames.app/.NameActivity t29 f}
12-15 17:04:42.767: I/ActivityManager(704): Timeline: Activity_idle id: android.os.BinderProxy@41ae0c10 time:27575356
12-15 17:04:43.181: I/ActivityManager(434): Timeline: Activity_windows_visible id: ActivityRecord{41faaa98 u0 com.cyanogenmod.trebuchet/com.android.launcher3.Launcher t1} time:27575770