Android中的流程对话框

时间:2013-03-11 12:38:35

标签: android android-asynctask

由于我对Android中的这个“异步任务”概念不熟悉,我搜索了很多帖子并实现了。在我的应用程序when opening the application i am fetching lot of images from url,中,所以它的delaying开了很多。所以我有打开应用程序时有一个活动的想法。我试过以下内容:

我的活动代码:

       Gallery glly;
     public void onCreate(Bundle savedInstanceState) 
        { 

            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.galleryview);   

            new RestoreDBTask().execute();
        }
      class RestoreDBTask extends AsyncTask <Void, Void, Nodelist>
        {
            private ProgressDialog dialog;
            Context ctx;

            public RestoreDBTask( Context ctx) {

                this.ctx = ctx;

            }

            @Override
            protected void onPreExecute()
            {
                dialog = ProgressDialog.show(
                    ctx,"Please Wait","Loading...", true);
            }

            @Override
            protected Nodelist doInBackground(Void... params)
            {
                 final String URLHeading = "http://m.indiatoday.in/xml/stories/sections_listing.xml";

                 Heading parser = new Heading();       
                 String xmldata = parser.getXmlFromUrl(URLHeading);       
                 Document domelement = parser.getDomElement(xmldata);       
                 NodeList node = domelement.getElementsByTagName("item");  



                     context.getInstance().setAppVariable("flag", "Home");
                 adapter=new GalleryviewAdapter(NewspapperActivity.this);
//                  GalleryviewAdapter adapter= new GalleryviewAdapter();
                 adapter.getelement("http://m.bvdjujd.in/xml/stories/jusdjhs.xml",1);   


    return node;

    }
     @Override
            protected void onPostExecute(String result)
            {
Element e0 = (Element) node.item(0);
                    txt1.setText(parser.getValue(e0, "sectionname"));

                    Element e1 = (Element) node.item(1);
                    txt2.setText(parser.getValue(e1, "sectionname"));

                    Element e2 = (Element) node.item(2);
                    txt3.setText(parser.getValue(e2, "sectionname"));

                    Element e3 = (Element) node.item(3);
                    txt4.setText(parser.getValue(e3, "sectionname"));

                    Element e4 = (Element) node.item(4);

                    txt5.setText(parser.getValue(e4, "sectionname"));

  glly.setAdapter(adapter);
           DisplayMetrics metrics1 = new DisplayMetrics(); 
           getWindowManager().getDefaultDisplay().getMetrics(metrics1); 
           MarginLayoutParams mlp1 = (MarginLayoutParams) glly.getLayoutParams(); 
           mlp1.setMargins(-(metrics1.widthPixels/2+40),mlp1.topMargin,mlp1.rightMargin, mlp1.bottomMargin );


                dialog.dismiss();
            }


    }

我的logcat:

03-12 10:24:08.430: I/Process(487): Sending signal. PID: 487 SIG: 9
03-12 10:24:22.140: D/dalvikvm(511): GC freed 2643 objects / 197184 bytes in 168ms
03-12 10:24:24.800: I/MemoryCache(511): MemoryCache will use up to 6.0MB
03-12 10:24:24.810: W/dalvikvm(511): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
03-12 10:24:24.845: E/AndroidRuntime(511): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
03-12 10:24:24.932: E/AndroidRuntime(511): java.lang.RuntimeException: An error occured while executing doInBackground()
03-12 10:24:24.932: E/AndroidRuntime(511):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
03-12 10:24:24.932: E/AndroidRuntime(511):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-12 10:24:24.932: E/AndroidRuntime(511):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-12 10:24:24.932: E/AndroidRuntime(511):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-12 10:24:24.932: E/AndroidRuntime(511):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-12 10:24:24.932: E/AndroidRuntime(511):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-12 10:24:24.932: E/AndroidRuntime(511):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-12 10:24:24.932: E/AndroidRuntime(511):  at java.lang.Thread.run(Thread.java:1096)
03-12 10:24:24.932: E/AndroidRuntime(511): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-12 10:24:24.932: E/AndroidRuntime(511):  at android.os.Handler.<init>(Handler.java:121)
03-12 10:24:24.932: E/AndroidRuntime(511):  at com.newspapper.ImageLoader.<init>(ImageLoader.java:29)
03-12 10:24:24.932: E/AndroidRuntime(511):  at com.newspapper.GalleryviewAdapter.<init>(GalleryviewAdapter.java:52)
03-12 10:24:24.932: E/AndroidRuntime(511):  at com.newspapper.NewspapperActivity$RestoreDBTask.doInBackground(NewspapperActivity.java:569)
03-12 10:24:24.932: E/AndroidRuntime(511):  at com.newspapper.NewspapperActivity$RestoreDBTask.doInBackground(NewspapperActivity.java:1)
03-12 10:24:24.932: E/AndroidRuntime(511):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-12 10:24:24.932: E/AndroidRuntime(511):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-12 10:24:24.932: E/AndroidRuntime(511):  ... 4 more
03-12 10:24:25.000: I/dalvikvm(511): threadid=7: reacting to signal 3
03-12 10:24:25.214: I/dalvikvm(511): Wrote stack trace to '/data/anr/traces.txt'

请帮我解决这个问题。请帮助我,不管我是否采取正确的行动......谢谢提前..

1 个答案:

答案 0 :(得分:2)

你正在

  

对于RestoreDBTask类型

,方法findViewById(int)未定义

因为您正在尝试从RestoreDBTask类访问UI元素。您可以使用Activity上下文而不是RestoreDBTask解决此问题:

TextView txt1 = (TextView)Your_Activity_Name.this.findViewById(R.id.Text01);
TextView txt2 = (TextView)Your_Activity_Name.this.findViewById(R.id.Text02);
TextView txt3 = (TextView)Your_Activity_Name.this.findViewById(R.id.Text03);
//....

但是当您尝试运行当前应用时,您将收到以下错误:

  

alledFromWrongThreadException:只创建了一个原始线程   视图层次结构可以触及其视图。

要避免在Streps后出现此错误:

1。首先从doInBackground内的网络服务获取数据。

2。NodeList node方法

返回doInBackground

3. 最终在onPostExecute中获取NodeList并执行xml解析然后访问或更新UI元素

示例: -

 class RestoreDBTask extends AsyncTask <Void, Void, NodeList>
    {
    ///.. your code here.....
        @Override
        protected NodeList doInBackground(Void... params)
        {

           //....your code here..
           return node; 
         }
 @Override
        protected void onPostExecute(NodeList result)
        {
            // get data from result
             ....
            // access UI elements here add value to it
            TextView txt1 = (TextView)Your_Activity_Name.
                                           this.findViewById(R.id.Text01);
            TextView txt2 = (TextView)Your_Activity_Name.
                                           this.findViewById(R.id.Text02);
            ......
            dialog.dismiss();
        }
 }