Progress Dialog在android中使用处理程序不能很好地工作

时间:2013-02-22 07:54:31

标签: android listview handler progressdialog

我正在一个应用程序中工作,我已经分配了进度对话框,但在完成加载后,单击某个编辑框后列表视图也不可见列表视图可见..

这是我的代码:

        new Thread() 
        {
            public void run() 
            {
             try
                {
                URL userurl = new URL("https://api.....");
                BufferedReader in = new BufferedReader(new InputStreamReader(userurl.openStream()));
                String inputLine;
                String strJson1 = "";
                while ((inputLine = in.readLine()) != null) 
                {
                    strJson1 += inputLine;
                }

                System.out.println("buffer"+strJson1);

                jsonobj = new JSONObject(strJson1);

            questionarray = jsonobj.getJSONArray("items");


                } catch (MalformedURLException tagobject) {
                    // TODO Auto-generated catch block
                    tagobject.printStackTrace();
                } catch (IOException tagobject) {
                    // TODO Auto-generated catch block
                    tagobject.printStackTrace();
                }catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    }
     handler.sendEmptyMessage(0);

    }
}.start();
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
    try
    {
        for (int i = 0;  i<questionarray.length(); i++) {

            ///Perform the operation get the data from WebService...



}
    catch(Exception e)
{
System.out.println(e);
}

}
};

错误:

02-23 16:32:10.178: E/AndroidRuntime(478): FATAL EXCEPTION: AsyncTask #1
02-23 16:32:10.178: E/AndroidRuntime(478): java.lang.RuntimeException: An error occured while executing doInBackground()
02-23 16:32:10.178: E/AndroidRuntime(478):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-23 16:32:10.178: E/AndroidRuntime(478):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
02-23 16:32:10.178: E/AndroidRuntime(478):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
02-23 16:32:10.178: E/AndroidRuntime(478):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
02-23 16:32:10.178: E/AndroidRuntime(478):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-23 16:32:10.178: E/AndroidRuntime(478):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-23 16:32:10.178: E/AndroidRuntime(478):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-23 16:32:10.178: E/AndroidRuntime(478):  at java.lang.Thread.run(Thread.java:1019)
02-23 16:32:10.178: E/AndroidRuntime(478): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
02-23 16:32:10.178: E/AndroidRuntime(478):  at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
02-23 16:32:10.178: E/AndroidRuntime(478):  at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
02-23 16:32:10.178: E/AndroidRuntime(478):  at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
02-23 16:32:10.178: E/AndroidRuntime(478):  at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
02-23 16:32:10.178: E/AndroidRuntime(478):  at android.view.View.invalidate(View.java:5279)
02-23 16:32:10.178: E/AndroidRuntime(478):  at android.widget.AbsListView.resetList(AbsListView.java:1120)
02-23 16:32:10.178: E/AndroidRuntime(478):  at android.widget.ListView.resetList(ListView.java:511)
02-23 16:32:10.178: E/AndroidRuntime(478):  at android.widget.ListView.setAdapter(ListView.java:440)
02-23 16:32:10.178: E/AndroidRuntime(478):  at com.example.stack.MainActivity.Callpagedetails(MainActivity.java:266)
02-23 16:32:10.178: E/AndroidRuntime(478):  at com.example.stack.MainActivity$GetTask.doInBackground(MainActivity.java:104)
02-23 16:32:10.178: E/AndroidRuntime(478):  at com.example.stack.MainActivity$GetTask.doInBackground(MainActivity.java:1)
02-23 16:32:10.178: E/AndroidRuntime(478):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-23 16:32:10.178: E/AndroidRuntime(478):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-23 16:32:10.178: E/AndroidRuntime(478):  ... 4 more
02-23 16:32:11.048: E/WindowManager(478): Activity com.example.stack.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4051f750 that was originally added here
02-23 16:32:11.048: E/WindowManager(478): android.view.WindowLeaked: Activity com.example.stack.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4051f750 that was originally added here
02-23 16:32:11.048: E/WindowManager(478):   at android.view.ViewRoot.<init>(ViewRoot.java:258)
02-23 16:32:11.048: E/WindowManager(478):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
02-23 16:32:11.048: E/WindowManager(478):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-23 16:32:11.048: E/WindowManager(478):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
02-23 16:32:11.048: E/WindowManager(478):   at android.app.Dialog.show(Dialog.java:241)
02-23 16:32:11.048: E/WindowManager(478):   at android.app.ProgressDialog.show(ProgressDialog.java:107)
02-23 16:32:11.048: E/WindowManager(478):   at android.app.ProgressDialog.show(ProgressDialog.java:90)
02-23 16:32:11.048: E/WindowManager(478):   at android.app.ProgressDialog.show(ProgressDialog.java:85)
02-23 16:32:11.048: E/WindowManager(478):   at com.example.stack.MainActivity$GetTask.onPreExecute(MainActivity.java:96)
02-23 16:32:11.048: E/WindowManager(478):   at android.os.AsyncTask.execute(AsyncTask.java:391)
02-23 16:32:11.048: E/WindowManager(478):   at com.example.stack.MainActivity.onCreate(MainActivity.java:88)
02-23 16:32:11.048: E/WindowManager(478):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-23 16:32:11.048: E/WindowManager(478):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-23 16:32:11.048: E/WindowManager(478):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-23 16:32:11.048: E/WindowManager(478):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-23 16:32:11.048: E/WindowManager(478):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-23 16:32:11.048: E/WindowManager(478):   at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 16:32:11.048: E/WindowManager(478):   at android.os.Looper.loop(Looper.java:123)
02-23 16:32:11.048: E/WindowManager(478):   at android.app.ActivityThread.main(ActivityThread.java:3683)
02-23 16:32:11.048: E/WindowManager(478):   at java.lang.reflect.Method.invokeNative(Native Method)
02-23 16:32:11.048: E/WindowManager(478):   at java.lang.reflect.Method.invoke(Method.java:507)
02-23 16:32:11.048: E/WindowManager(478):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-23 16:32:11.048: E/WindowManager(478):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-23 16:32:11.048: E/WindowManager(478):   at dalvik.system.NativeStart.main(Native Method)

有人能帮帮我..

1 个答案:

答案 0 :(得分:3)

首先使用单独的工作线程编写Rest客户端是错误的。

根据Virgil Dobjanschi的vedio编写的webservice。 http://www.youtube.com/watch?v=xHXn3Kg2IQE。他说不要写在单独的工作线程中。 因此,为了使您的应用程序保持一致,您可以使用Android Rest Client的服务。

你可以参考  http://www.codeproject.com/Articles/429997/Sample-Implementation-of-Virgil-Dobjanschis-Rest-p用于编写Android webservice Client的基础。