0

我正在编写一个必须联系网络服务的应用程序。它正在工作。但是,现在我必须使用异步任务。我必须填写类别列表。我不知道有什么问题。

异步类:

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 功能:

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

堆栈跟踪 :

 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

问候。

4

2 回答 2

1

用这个改变你的

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

其次,您不能show()onPreExecute. 我建议你Dialog在执行任务之前展示你的。

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

无法添加窗口

这个错误通常会导致getApplicationContext().

于 2012-06-29T13:28:59.487 回答
0

从错误中,看起来好像ProgressDialog是在实际创建主窗口之前启动的。

尝试将 放在AsyncTask最末尾OnCreate()或将其放在 中onResume()以确保在任务开始之前已完全创建 UI。

这可能与这篇文章的性质相似(但不完全相同)。

于 2012-06-29T13:20:29.563 回答