从runnable / TimerTask中检索字符串

时间:2010-08-06 13:57:54

标签: java android timer runnable

我已经实现了一个每15分钟解析一次URL的计时器(Timer任务)。 我创建的对象获取该数据。我之后使用它来在屏幕上显示数据。

现在,每当我尝试从runnable中检索Object / a String = Object.toString()时,我会得到空指针异常和致命错误。

我的问题是,是否有可能使用其他技术来完成它,或者对象不再存在于runnable中,我们无能为力;如果是这样,任何人都可以告诉我如果还有另一种实现计时器/ runnable的方法吗?

非常感谢

这是我的大部分代码,我有一个问题

 @Override
    public void onCreate(Bundle icicle) {
            super.onCreate(icicle)
           final  TextView tv = new TextView(this);
            TimerTask scanTask;
            final Handler handler = new Handler();
            Timer t = new Timer();                  
                scanTask = new TimerTask() {
                    public void run() {
                            handler.post(new Runnable() {
                                    public void run() {
                                        URL url = null;
                                        try {
                                            url = new URL("http://www.eurosport.fr/");
                                        } catch (MalformedURLException e3) {

                                            e3.printStackTrace();
                                        }

                                        /* Get a SAXParser from the SAXPArserFactory. */
                                        SAXParserFactory spf = SAXParserFactory.newInstance();
                                        SAXParser sp;
                                        try {
                                            sp = spf.newSAXParser();
                                        } catch (ParserConfigurationException e2) {

                                            e2.printStackTrace();
                                        } catch (SAXException e2) {

                                            e2.printStackTrace();
                                        }

                                        /* Get the XMLReader of the SAXParser we created. */
                                        XMLReader xr = null;
                                        try {
                                            sp = spf.newSAXParser();
                                            xr = sp.getXMLReader();
                                        } catch (SAXException e1) {

                                            e1.printStackTrace();
                                        } catch (ParserConfigurationException e) {

                                            e.printStackTrace();
                                        }
                                        /* Create a new ContentHandler and apply it to the XML-Reader*/
                                        ExampleHandler myExampleHandler = new ExampleHandler();
                                        try {
                                            sp = spf.newSAXParser();
                                        } catch (ParserConfigurationException e1) {
                                            // TODO Auto-generated catch block
                                            e1.printStackTrace();
                                        } catch (SAXException e1) {
                                            // TODO Auto-generated catch block
                                            e1.printStackTrace();
                                        }
                                        xr.setContentHandler(myExampleHandler);

                                        /* Parse the xml-data from our URL. */
                                        try {
                                            xr.parse(new InputSource(url.openStream()));
                                        } catch (IOException e) {

                                            e.printStackTrace();
                                        } catch (SAXException e) {

                                            e.printStackTrace();
                                        }
                                        /* Parsing has finished. */

                                        /* Our ExampleHandler now provides the parsed data to us. */
                                        ParsedExampleDataSet parsedExampleDataSet =
                                                                                        myExampleHandler.getParsedData();

                                       System.out.println(parsedExampleDataSet.toString());

                                        tv.setText(parsedExampleDataSet.toString());


                                     Context context = this.getBaseContext(); 

 // I also dont understand why inside the runnable getBaseContext() does not exist ???

    Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),
            R.raw.nature1)
        context.setWallpaper(mBitmap);


                                    }


                           });

                    }  };
                    // I want to retrieve ParsedExampleDataSEt here in order to use it  is it Possible ????



                    this.setContentView(tv);



                   long temps=1*15*1000;

                t.scheduleAtFixedRate(scanTask, 300,temps ); 

1 个答案:

答案 0 :(得分:2)

潜在的丑陋方法: 扩展TimerTask并制作一个抽象方法,如

public abstract void onUrlRetrivalFinished(String data);

创建TimerTask对象时,您现在可以对该方法进行匿名实现,并在该方法中处理检索到的数据。

(在我看来)不那么丑陋的方法:

制作如下界面:

public interface UrlRetrivalListener {
    public void onUrlRetrivalFinished(String data);
}

子类TimerTask并创建一个如下字段:

private UrlRetrivalListener listener;

现在实现上面提到的侦听器接口,在该接口中处理检索到的String。将侦听器作为参数传递给TimerTask,甚至让TimerTask拥有多个侦听器,并在检索/解析所需数据时,只需调用侦听器onUrlRetrivalFinished()方法。

这应该可以解决问题,但更多信息会很好。

相关问题