进度对话框和AsyncTask错误

时间:2013-03-05 04:44:27

标签: android android-asynctask progressdialog

我对AsyncTask和ProgressDialog有点新意,每当我在我的按钮上调用new MyTask().execute();时我的方法是不对的,我得到一个空指针异常错误?我的代码出了什么问题或者我错过了什么?

仅应处理的流程取决于所选的当前TabHost ..

这是我的代码:

public class MyTask extends AsyncTask<Void, Integer, Void>{
                @Override
                protected void onPreExecute() {
                    //Show progress Dialog here
                    super.onPreExecute();
                    progress.show();
                }
                @Override
                protected void onPostExecute(Void result) {
                    super.onPostExecute(result);
                    //Update UI here if needed
                    //Dismiss Progress Dialog here
                    progress.dismiss();
                }
                @Override
                protected Void doInBackground(Void... params) {
                    SparseBooleanArray laptopBooleanArray = laptopLV.getCheckedItemPositions();
                    SparseBooleanArray mp3BooleanArray = mp3LV.getCheckedItemPositions();
                    SparseBooleanArray mobilepBooleanArray = mobileLV.getCheckedItemPositions();
                    switch (th.getCurrentTab()) {
                    case 0:          
                        for(int i = 0; i < laptopLV.getCount(); i++)
                        {
                            if(laptopBooleanArray.get(i) == true) 
                            {
                                selectedFromLaptopList.add(laptopLV.getItemAtPosition(i).toString());
                            }
                        }
                        handler.setItemListData(TechiesActivity.this, "Techies", selectedFromLaptopList, "Unit" , "Laptop" , "Store");
                        displayView();
                        drawer.closeMenu();
                        selectedFromLaptopList.clear(); 

                        break;
                    case 1:
                        for(int i = 0; i < mp3LV.getCount(); i++)
                        {
                            if(mp3BooleanArray.get(i) == true) 
                            {
                                selectedFromLaptopList.add(mp3LV.getItemAtPosition(i).toString());
                            }
                        }
                        handler.setItemListData(TechiesActivity.this, "Techies", selectedFromMp3List, "Unit" , "Mp3" , "Store");
                        displayView();
                        drawer.closeMenu();
                        selectedFromLaptopList.clear(); 
                        break;
                    case 2:
                        for(int i = 0; i < mobileLV.getCount(); i++)
                        {
                            if(mobilepBooleanArray.get(i) == true) 
                            {
                                selectedFromLaptopList.add(mobileLV.getItemAtPosition(i).toString());
                            }
                        }
                        handler.setItemListData(TechiesActivity.this, "Techies", selectedFromMobileList, "Unit" , "Mobile" , "Store");
                        displayView();
                        drawer.closeMenu();
                        selectedFromLaptopList.clear();
                        break;
                    }
                    return null;
                }      
            }

这是LogCat错误:

03-05 13:15:05.650: E/AndroidRuntime(497): FATAL EXCEPTION: AsyncTask #1
03-05 13:15:05.650: E/AndroidRuntime(497): java.lang.RuntimeException: An error occured while executing doInBackground()
03-05 13:15:05.650: E/AndroidRuntime(497):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
03-05 13:15:05.650: E/AndroidRuntime(497):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-05 13:15:05.650: E/AndroidRuntime(497):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-05 13:15:05.650: E/AndroidRuntime(497):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-05 13:15:05.650: E/AndroidRuntime(497):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-05 13:15:05.650: E/AndroidRuntime(497):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-05 13:15:05.650: E/AndroidRuntime(497):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-05 13:15:05.650: E/AndroidRuntime(497):  at java.lang.Thread.run(Thread.java:1096)
03-05 13:15:05.650: E/AndroidRuntime(497): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
03-05 13:15:05.650: E/AndroidRuntime(497):  at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
03-05 13:15:05.650: E/AndroidRuntime(497):  at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
03-05 13:15:05.650: E/AndroidRuntime(497):  at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
03-05 13:15:05.650: E/AndroidRuntime(497):  at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
03-05 13:15:05.650: E/AndroidRuntime(497):  at android.view.View.invalidate(View.java:5139)
03-05 13:15:05.650: E/AndroidRuntime(497):  at android.widget.AbsListView.resetList(AbsListView.java:1011)
03-05 13:15:05.650: E/AndroidRuntime(497):  at android.widget.ListView.resetList(ListView.java:493)
03-05 13:15:05.650: E/AndroidRuntime(497):  at android.widget.ListView.setAdapter(ListView.java:422)
03-05 13:15:05.650: E/AndroidRuntime(497):  at com.shop.browser.TechiesActivity.displayView(TechiesActivity.java:98)
03-05 13:15:05.650: E/AndroidRuntime(497):  at com.shop.browser.TechiesActivity$MyTask.doInBackground(TechiesActivity.java:257)
03-05 13:15:05.650: E/AndroidRuntime(497):  at com.shop.browser.TechiesActivity$MyTask.doInBackground(TechiesActivity.java:1)
03-05 13:15:05.650: E/AndroidRuntime(497):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-05 13:15:05.650: E/AndroidRuntime(497):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-05 13:15:05.650: E/AndroidRuntime(497):  ... 4 more
03-05 13:15:07.850: E/WindowManager(497): Activity com.shop.browser.TechiesActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f93528 that was originally added here
03-05 13:15:07.850: E/WindowManager(497): android.view.WindowLeaked: Activity com.shop.browser.TechiesActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f93528 that was originally added here
03-05 13:15:07.850: E/WindowManager(497):   at android.view.ViewRoot.<init>(ViewRoot.java:247)
03-05 13:15:07.850: E/WindowManager(497):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
03-05 13:15:07.850: E/WindowManager(497):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-05 13:15:07.850: E/WindowManager(497):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
03-05 13:15:07.850: E/WindowManager(497):   at android.app.Dialog.show(Dialog.java:241)
03-05 13:15:07.850: E/WindowManager(497):   at com.shop.browser.TechiesActivity$MyTask.onPreExecute(TechiesActivity.java:233)
03-05 13:15:07.850: E/WindowManager(497):   at android.os.AsyncTask.execute(AsyncTask.java:391)
03-05 13:15:07.850: E/WindowManager(497):   at com.shop.browser.TechiesActivity.onClick(TechiesActivity.java:181)
03-05 13:15:07.850: E/WindowManager(497):   at java.lang.reflect.Method.invokeNative(Native Method)
03-05 13:15:07.850: E/WindowManager(497):   at java.lang.reflect.Method.invoke(Method.java:521)
03-05 13:15:07.850: E/WindowManager(497):   at android.view.View$1.onClick(View.java:2067)
03-05 13:15:07.850: E/WindowManager(497):   at android.view.View.performClick(View.java:2408)
03-05 13:15:07.850: E/WindowManager(497):   at android.view.View$PerformClick.run(View.java:8816)
03-05 13:15:07.850: E/WindowManager(497):   at android.os.Handler.handleCallback(Handler.java:587)
03-05 13:15:07.850: E/WindowManager(497):   at android.os.Handler.dispatchMessage(Handler.java:92)
03-05 13:15:07.850: E/WindowManager(497):   at android.os.Looper.loop(Looper.java:123)
03-05 13:15:07.850: E/WindowManager(497):   at android.app.ActivityThread.main(ActivityThread.java:4627)
03-05 13:15:07.850: E/WindowManager(497):   at java.lang.reflect.Method.invokeNative(Native Method)
03-05 13:15:07.850: E/WindowManager(497):   at java.lang.reflect.Method.invoke(Method.java:521)
03-05 13:15:07.850: E/WindowManager(497):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-05 13:15:07.850: E/WindowManager(497):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-05 13:15:07.850: E/WindowManager(497):   at dalvik.system.NativeStart.main(Native Method)

按要求的按钮代码:

public void onClick (View v) {
        new MyTask().execute();
    }

任何意见或回答都将不胜感激。

3 个答案:

答案 0 :(得分:2)

使用MyTask类构造函数传递Activity Context来初始化ProgressDialog和你希望在onPreExecute内的ProgressDialog中显示的消息:

public class MyTask extends AsyncTask<Void, Integer, Void>{

Context context;
String str_mesg;
ProgressDialog progress;
 public MyTask(Context context, String str_mesg){
  this.context=context;
  this.str_mesg=str_mesg;
 }
@Override
  protected void onPreExecute() {
    //Show progress Dialog here
      super.onPreExecute();

      // create ProgressDialog here ...
      progress = new ProgressDialog(context);
      progress.setMessage(str_mesg);
      // set other progressbar attributes
       ....
      progress.show();

    }

 // your code here....

启动AsyncTask,如按钮点击:

button.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
       // TODO Auto-generated method stub
        new MyTask(Your_Current_Activity.this).execute(); 
     }
 });

答案 1 :(得分:1)

听起来像进度对话框没有初始化。所以只需确保它已正确初始化。

protected void onPreExecute() {
                    super.onPreExecute();
                    //initialize progress dialog here
                    progress.show();
                }

编辑

class YourClass extends AsyncTask<...>
{
    ProgressDialog progress;

    Protected void onPreExecute(){
        // create dialog here
      progress = new ProgressDialog (...);
      progress.setMessage(...);
      progress.show();

    }
    protected void onPostExecute(...){
        // dismiss dialog here
        progress.dismiss();
    }
}

答案 2 :(得分:0)

其他部分,我希望它有帮助... 如果你想在后台任务中更改文本和进度百分比你可以定义一个处理程序

public class MyTask extends AsyncTask<Void, Integer, Void>
{
    private ProgressDialog progressDialog;
    private String mstepDescription;
    protected int mprogressPercentage;

    private String mstepDescription;
    protected int mprogressPercentage;

    Handler handle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            progressDialog.setMessage(mstepDescription);
            progressDialog.setProgress(mprogressPercentage);

        }
    };

    void Step (String StepDescription, int Progress)
    {
        mstepDescription = StepDescription;
        mprogressPercentage = Progress;
       handle.sendMessage(handle.obtainMessage());
    }


    Protected void onPreExecute()
    {
         // create dialog here
        progress = new ProgressDialog (...);
        progress.setMessage(...);
        progress.show();
     }

     protected void onPostExecute(...){
        // dismiss dialog here
        progress.dismiss();
     }

   @Override
    protected Void doInBackground(Void... params)
    {
          // do somthing 
          Step("...", 40);
          // do something else
    }

}