应用程序关闭时使用异步方法

时间:2012-06-29 13:03:59

标签: android

我正在撰写一个必须联系网络服务的应用程序。 它正在发挥作用。 但是,现在我必须使用异步任务。我必须填写一个类别列表。 我无法确定问题是什么。

异步类:

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


    }

电话:

    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

问候。

2 个答案:

答案 0 :(得分:1)

使用此

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

其次,您无法在show()中调用onPreExecute方法。我建议您在执行任务之前显示Dialog

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

无法添加窗口

此错误通常会导致getApplicationContext()

答案 1 :(得分:0)

从错误中看,在实际创建主窗口之前,ProgressDialog似乎正在启动。

尝试将AsyncTask放在OnCreate()的最后或将其放在onResume()中,以确保在任务开始之前已完全创建用户界面。

这可能与此post性质相似(但不完全相同)。