0

I'm writting an application which has to contact a webservice. It's working. But, now I have to use the async task. I have to fill a list of category. I can't figure what's the problem.

The async class :

private class CallCategory extends AsyncTask<List<Category>,Void,List<Category>>{
        private ProgressDialog dialog;


        protected void onPreExecute() {
            this.dialog = ProgressDialog.show(getApplicationContext(), "Calling", "Time Service...", true);
        }

        protected void onPostExecute() {
            this.dialog.cancel();
        }

        @Override
        protected List<Category> doInBackground(List<Category>... params) {
            return ServerCall.GetCategory();
        }


    }

The call :

    CallCategory cc = new CallCategory();
    _ListCategory = new ArrayList<Category>();
    cc.execute();

GetCategory fonction :

public static List<Category> GetCategory (){
    List<Category> categories = null;
    try{

        String url =  "http://188.130.40.103/api/"+"Category";
        RestClient client = new RestClient(url);

        //Call
        client.Execute(RequestMethod.GET);

        if(client.getResponseCode() == 200){

        //Get the response
        String response = client.getResponse();

        //build list of categories
        Type listType = new TypeToken<ArrayList<Category>>() {
    }.getType();

     categories = new Gson().fromJson(response, listType);
    }

    }
    catch (Exception e) {
        Log.i("Error", e.toString());
    }
    return categories;
}

StackTrace :

 06-29 12:58:57.746: W/dalvikvm(3087): threadid=3: thread exiting with
     uncaught exception (group=0x4001b188) 06-29 12:58:57.746:
     E/AndroidRuntime(3087): Uncaught handler: thread main exiting due to
     uncaught exception 06-29 12:58:57.786: E/AndroidRuntime(3087):
     java.lang.RuntimeException: Unable to start activity
     ComponentInfo{ApPicture.Android/ApPicture.Android.ApPictureActivity}:
     android.view.WindowManager$BadTokenException: Unable to add window --
     token null is not for an application 06-29 12:58:57.786:
     E/AndroidRuntime(3087):    at
     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
     06-29 12:58:57.786: E/AndroidRuntime(3087):    at
     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
     06-29 12:58:57.786: E/AndroidRuntime(3087):    at
     android.app.ActivityThread.access$2200(ActivityThread.java:119) 06-29
     12:58:57.786: E/AndroidRuntime(3087):  at
     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
     06-29 12:58:57.786: E/AndroidRuntime(3087):    at
     android.os.Handler.dispatchMessage(Handler.java:99) 06-29
     12:58:57.786: E/AndroidRuntime(3087):  at
     android.os.Looper.loop(Looper.java:123) 06-29 12:58:57.786:
     E/AndroidRuntime(3087):    at
     android.app.ActivityThread.main(ActivityThread.java:4363) 06-29
     12:58:57.786: E/AndroidRuntime(3087):  at
     java.lang.reflect.Method.invokeNative(Native Method) 06-29
     12:58:57.786: E/AndroidRuntime(3087):  at
     java.lang.reflect.Method.invoke(Method.java:521) 06-29 12:58:57.786:
     E/AndroidRuntime(3087):    at
     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
     06-29 12:58:57.786: E/AndroidRuntime(3087):    at
     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 06-29
     12:58:57.786: E/AndroidRuntime(3087):  at
     dalvik.system.NativeStart.main(Native Method) 06-29 12:58:57.786:
     E/AndroidRuntime(3087): Caused by:
     android.view.WindowManager$BadTokenException: Unable to add window --
     token null is not for an application 06-29 12:58:57.786:
     E/AndroidRuntime(3087):    at
     android.view.ViewRoot.setView(ViewRoot.java:472) 06-29 12:58:57.786:
     E/AndroidRuntime(3087):    at
     android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
     06-29 12:58:57.786: E/AndroidRuntime(3087):    at
     android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
     06-29 12:58:57.786: E/AndroidRuntime(3087):    at
     android.app.Dialog.show(Dialog.java:239) 06-29 12:58:57.786:
     E/AndroidRuntime(3087):    at
     android.app.ProgressDialog.show(ProgressDialog.java:107) 06-29
     12:58:57.786: E/AndroidRuntime(3087):  at
     android.app.ProgressDialog.show(ProgressDialog.java:90) 06-29
     12:58:57.786: E/AndroidRuntime(3087):  at
     ApPicture.Android.ApPictureActivity$CallCategory.onPreExecute(ApPictureActivity.java:406)
     06-29 12:58:57.786: E/AndroidRuntime(3087):    at
     android.os.AsyncTask.execute(AsyncTask.java:391) 06-29 12:58:57.786:
     E/AndroidRuntime(3087):    at
     ApPicture.Android.ApPictureActivity.LoadCategory(ApPictureActivity.java:291)
     06-29 12:58:57.786: E/AndroidRuntime(3087):    at
     ApPicture.Android.ApPictureActivity.onCreate(ApPictureActivity.java:108)
     06-29 12:58:57.786: E/AndroidRuntime(3087):    at
     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
     06-29 12:58:57.786: E/AndroidRuntime(3087):    at
     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
     06-29 12:58:57.786: E/AndroidRuntime(3087):    ... 11 more

Regards.

Vipul
  • 27,808
  • 7
  • 60
  • 75
David
  • 1,679
  • 11
  • 22
  • Can you post a java stack trace? I assume you mean that it is "force closing", and if so, then the logcat exception should show you the line that it is failing on. – stuckless Jun 29 '12 at 13:07
  • show code of GetCategory function & also post your logs – Vipul Jun 29 '12 at 13:08
  • What part of the Activity is cc.executed() from... ie, onCreate(), etc. – stuckless Jun 29 '12 at 13:11
  • @stuckless yes, in the onCreate() I call a fonction which has to do cc.execute. – David Jun 29 '12 at 13:13
  • @David, I've posted an answer that I think might be relevant. I don't think your dialog is attempting to be shown before the main window is created. – stuckless Jun 29 '12 at 13:39

2 Answers2

1

Change yours with this

this.dialog = ProgressDialog.show(YourActivity.this, "Calling", "Time Service...", true);

And second, you can't call show() method in onPreExecute. I recommend to you to show your Dialog before you execute a task.

...
dialog.show();
task.execute();
...

Unable to add window

This error usually cause getApplicationContext().

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
0

From the Error, it looks like the ProgressDialog is being started before the Main window is actually created.

Try putting the AsyncTask at the very end of the OnCreate() or put it in the onResume() to ensure that the UI has been fully created before the task starts.

This may be similar in nature (but not exactly the same) as this post.

Community
  • 1
  • 1
stuckless
  • 6,515
  • 2
  • 19
  • 27