0

由于我是 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();
            }


    }

我的日志猫:

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'

请帮我解决这个问题。也帮我看看我是否做对了..提前谢谢..

4

1 回答 1

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:只有创建视图层次结构的原始线程才能接触其视图。

f为避免此错误,您将需要以下 Streps :

1.首先从webservice里面获取数据doInBackground

2.NodeList nodedoInBackground方法返回

3.最后获取 NodeListonPostExecute并进行 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();
        }
 }
于 2013-03-11T12:46:04.613 回答