简体   繁体   中英

Progress dialog and AsyncTask error

I'm a bit new with the AsyncTask and ProgressDialog and i'm getting a null pointer exception error whenever i call new MyTask().execute(); on my button does my approach is not right? what is wrong with my code or what am i missing?

The process only it should process depends on the current TabHost selected so..

Here is my code:

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;
                }      
            }

and here is the LogCat error:

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)

Code for the button as requested:

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

Any comments or answer will be appreciated.

use class MyTask constructor for passing Activity Context to initialize ProgressDialog and message which u want to show in ProgressDialog inside onPreExecute as :

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

and start AsyncTask as on Button Click :

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

Sounds like progress dialog is not getting initialized .So just make sure it is initialized correctly.

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

EDIT :

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

nother part that I hope it helps ... if u want to change the text and percentage of progress during the background task u can define a handler

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
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM