0

我对 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();
    }

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

4

3 回答 3

2

使用类 MyTask 构造函数来传递 Activity 上下文来初始化 ProgressDialog 和你想在 ProgressDialog 里面显示的消息 onPreExecute

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....

并在 Button Click 上启动 AsyncTask :

button.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
       // TODO Auto-generated method stub
        new MyTask(Your_Current_Activity.this).execute(); 
     }
 });
于 2013-03-05T05:00:42.313 回答
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();
    }
}
于 2013-03-05T05:01:53.107 回答
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
    }

}

于 2014-10-08T17:16:14.233 回答