从方法(可运行)计时器中获取对象

发布于 2024-09-13 20:53:54 字数 5178 浏览 2 评论 0原文

我实现了一个计时器,每 15 分钟解析一次 URL(Timer 任务)。 名为 ParsedExampleDataSet 的对象获取该数据。

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

我怎样才能取回它?我可以尝试另一种实现吗? 为什么 getBaseContext() 在可运行对象中不起作用?

这是我遇到问题的大部分代码。我还在出现问题的地方添加了两条评论。

 @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 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 15 min (the Timer task).
An object called ParsedExampleDataSet gets that data.

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

How can I retrieve it? Is there another implementation that I could try?
Why does getBaseContext() not work inside the runnable?

Here is most of my code where I have a problem. I also added two comments where my problem occurs.

 @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 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 20:53:54

我认为这是因为当您在 TimerTask() 类中执行: Context context = this.getBaseContext() 时,您引用的变量超出了班级。由于 TimerTask() 不是 ContextWrapper 的子类,因此它不会直接获取上下文。要获取活动的上下文(希望不是应用程序的上下文),您应该执行以下操作:Context context = ParsedExampleDataSet.this.getBaseContext(); 这样您就可以可能不应该出现任何空指针异常。

I think it's because when you do: Context context = this.getBaseContext() inside the TimerTask() class, you are referring to a variable which is out of the scope of the class. Because TimerTask() doesn't subclass from ContextWrapper, it doesn't get the context directly. To get the context of the activity (hopefully! and not Application's context), you should do: Context context = ParsedExampleDataSet.this.getBaseContext(); That way you probably shouldn't get any null pointer exceptions.

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