从可运行/TimerTask 中检索字符串

发布于 2024-09-13 01:29:51 字数 5123 浏览 0 评论 0原文

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

现在,每当我尝试从可运行对象中检索该 Object/a String=Object.toString() 时,我都会收到空指针异常和致命错误。

我的问题是是否可以使用其他技术来做到这一点,或者对象不再存在于可运行对象之外,我们对此无能为力;如果是这种情况,任何人都可以告诉我是否有另一种实现计时器/可运行的方法?

非常感谢,

这是我遇到问题的大部分代码

 @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 ); 

I have implemented a timer that parses a URL every 15min (the Timer task).
An Object that i have created gets that data .I use it afterwards to display the data on the screen .

Now , whenever i try to retrieve that Object/a String=Object.toString() out of the runnable I get null pointer exception and fatal errors .

My question is whether it is possible to do using some other technique to do it , or The Object cease to exist out of the runnable and there is not much we can do about it ;if that is the case can anybody tell me if there is another way of implementing a timer/runnable ?

Thanks a Lot

here is most of my code where i have a problem

 @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 ); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原谅过去的我 2024-09-20 01:29:51

潜在的丑陋方法:
扩展 TimerTask 并创建一个抽象方法,例如

public abstract void onUrlRetrivalFinished(String data);

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

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

创建一个接口,例如:

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

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

private UrlRetrivalListener listener;

现在实现上述侦听器接口,在其中处理检索到的字符串。将侦听器作为参数传递给 TimerTask,甚至让 TimerTask 有多个侦听器,在检索/解析所需数据时,只需调用侦听器的 onUrlRetrivalFinished() 方法即可。

这应该可以解决问题,但如果能提供更多信息就更好了。

The potentially ugly method:
Extend TimerTask and make an abstract method such as

public abstract void onUrlRetrivalFinished(String data);

When you create the TimerTask object you can now make a anonymous implementation of the method, and in that method handle the retrieved data.

The (in my opinion) less ugly method:

Make an interface such as:

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

Sub-class TimerTask and make a field such as:

private UrlRetrivalListener listener;

Now make an implementation of the above mentioned listener interface, in which you handle the retrieved String. Pass the listener as a parameter to your TimerTask, or even let the TimerTask have more than one listener, and upon retrieving/parsing the data needed, you simply call the listeners onUrlRetrivalFinished() method.

This should do the trick, but some more information would be nice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文