你能帮助我弄明白为什么当我在onProgressUpdate
中调用asynctask
时,我总是得到空指针异常。以下是我的代码:
public class SyncBrand extends AsyncTask<String, String, Boolean>
{
private ProgressDialog progressDialog;
public static final int BRAND_DIALOG_DOWNLOAD_PROGRESS = 0;
public SyncBrand(Context context, String _username, String _password, String _code,String _remarks,String _date,String _province,String _infotype,
String _competitor,ArrayList<String> _brands, ArrayList<String> _segments)
{
...
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case BRAND_DIALOG_DOWNLOAD_PROGRESS:
progressDialog = new ProgressDialog(mContext);
progressDialog.setMessage("Updating Sub Brands..");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
progressDialog.show();
return progressDialog;
default:
return null;
}
}
@SuppressWarnings("deprecation")
protected void onPreExecute()
{
super.onPreExecute();
((Activity) mContext).showDialog(BRAND_DIALOG_DOWNLOAD_PROGRESS);
}
protected Boolean doInBackground(String... arg0)
{
//int count = 0;
try{
...
String subBrandId = "", subBrandName = "", brandId = "", IsActive = "";
for (int i = 0; i < newSubBrandCode.size(); i++)
{
subBrandId = newSubBrandCode.get(i);
subBrandName = newBrandname.get(i);
brandId = newBrandCode.get(i);
IsActive = newIsActive.get(i);
databaseHandler.updateSubBrand(new Cons_Sync(subBrandId, subBrandName, brandId, IsActive));
publishProgress("" + String.valueOf(0));
}
databaseHandler.close();
return true;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
progressDialog.setProgress(Integer.parseInt(progress[0]));
}
@SuppressWarnings("deprecation")
protected void onPostExecute(Boolean valid)
{
((Activity) mContext).removeDialog(BRAND_DIALOG_DOWNLOAD_PROGRESS);
if(valid){
...
}else{
...
}
}
}
下面是我的logcat:
09-30 14:10:26.285: E/AndroidRuntime(19139): FATAL EXCEPTION: main
09-30 14:10:26.285: E/AndroidRuntime(19139): java.lang.NullPointerException
09-30 14:10:26.285: E/AndroidRuntime(19139): at com.resd.ireport.SyncBrand.onProgressUpdate(SyncBrand.java:183)
09-30 14:10:26.285: E/AndroidRuntime(19139): at com.red.ireport.SyncBrand.onProgressUpdate(SyncBrand.java:1)
09-30 14:10:26.285: E/AndroidRuntime(19139): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:606)
09-30 14:10:26.285: E/AndroidRuntime(19139): at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 14:10:26.285: E/AndroidRuntime(19139): at android.os.Looper.loop(Looper.java:132)
09-30 14:10:26.285: E/AndroidRuntime(19139): at android.app.ActivityThread.main(ActivityThread.java:4123)
09-30 14:10:26.285: E/AndroidRuntime(19139): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 14:10:26.285: E/AndroidRuntime(19139): at java.lang.reflect.Method.invoke(Method.java:491)
09-30 14:10:26.285: E/AndroidRuntime(19139): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
09-30 14:10:26.285: E/AndroidRuntime(19139): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
09-30 14:10:26.285: E/AndroidRuntime(19139): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:4)
在onPreExcute方法中调用
((Activity) mContext).showDialog(BRAND_DIALOG_DOWNLOAD_PROGRESS);
尝试致电
onCreateDialog(BRAND_DIALOG_DOWNLOAD_PROGRESS)
你的问题将得到解决..!
答案 1 :(得分:1)
致电
progressDialog.setMax(INT_MAX_VALUE);
onCreateDialog()&amp;中的尝试
答案 2 :(得分:0)
尝试这个,如果这可以帮助......
publishProgress(“”+ String.valueOf(i));
如果问题仍然存在,请告诉我......