找对象了一个方法(可运行)定时器 [英] Get Object out of a method(runnable) timer

查看:179
本文介绍了找对象了一个方法(可运行)定时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了解析一个URL每15分钟(在定时任务),一个计时器。
ParsedExampleDataSet 称为对象获取的数据。

每当我尝试检索对象或字符串= Object.toString() 出可运行的,我得到一个空指针异常和致命错误。

我该如何找回?是否有另一种实现方式,我可以试试吗?
为什么 getBaseContext()可运行里面不起作用?

下面是我的大多数code的,我有一个问题。我还加了两个注释出现在哪里我的问题。

  @覆盖
    公共无效的onCreate(捆绑冰柱){
            super.onCreate(冰柱)
           最终的TextView电视=新的TextView(本);
            TimerTask的scanTask;
            最后的处理程序处理程序=新的处理程序();
            TIMER T =新的Timer();
                scanTask =新的TimerTask(){
                    公共无效的run(){
                            handler.post(新的Runnable(){
                                    公共无效的run(){
                                        网址URL = NULL;
                                        尝试{
                                            URL =新的URL(http://www.eurosport.fr/);
                                        }赶上(MalformedURLException的E3){                                            e3.printStackTrace();
                                        }                                        / *获取从一个SAXParserFactory的SAXParser的。 * /
                                        SPF SAXParserFactory的= SAXParserFactory.newInstance();
                                        SAXParser的SP;
                                        尝试{
                                            SP = spf.newSAXParser();
                                        }赶上(E2的ParserConfigurationException){                                            e2.printStackTrace();
                                        }赶上(E2的SAXException){                                            e2.printStackTrace();
                                        }                                        / *获取我们创建的SAXParser的的的XMLReader。 * /
                                        XMLReader的XR = NULL;
                                        尝试{
                                            SP = spf.newSAXParser();
                                            XR = sp.getXMLReader();
                                        }赶上(E1的SAXException){                                            e1.printStackTrace();
                                        }赶上(的ParserConfigurationException E){                                            e.printStackTrace();
                                        }
                                        / *创建一个新的ContentHandler,并将其应用到XML阅读器* /
                                        ExampleHandler myExampleHandler =新ExampleHandler();
                                        尝试{
                                            SP = spf.newSAXParser();
                                        }赶上(E1的ParserConfigurationException){
                                            // TODO自动生成catch块
                                            e1.printStackTrace();
                                        }赶上(E1的SAXException){
                                            // TODO自动生成catch块
                                            e1.printStackTrace();
                                        }
                                        xr.setContentHandler(myExampleHandler);                                        / *从我们的网址解析XML数据。 * /
                                        尝试{
                                            xr.parse(新的InputSource(url.openStream()));
                                        }赶上(IOException异常五){                                            e.printStackTrace();
                                        }赶上(SAXException的E){                                            e.printStackTrace();
                                        }
                                        / *解析完成。 * /                                        / *我们ExampleHandler现在提供解析的数据给我们。 * /
                                        ParsedExampleDataSet parsedExampleDataSet =
                                                                                        myExampleHandler.getParsedData();                                       的System.out.println(parsedExampleDataSet.toString());                                        tv.setText(parsedExampleDataSet.toString());
                                     上下文的背景下= this.getBaseContext(); //我不明白为什么可运行getBaseContext()内不存在?    位图mBitmap = BitmapFactory.de codeResource(getResources()
            R.raw.nature1)
        context.setWallpaper(mBitmap);
                                    }
                           });                    }};
                    //我要为了使用它,才有可能在这里找回ParsedExampleDataSEt ????                    this.setContentView(电视);                   长期临时工= 1 * 15 * 1000;                t.scheduleAtFixedRate(scanTask,300,临时工);


解决方案

我想这是因为当你这样做:上下文的背景下= this.getBaseContext()中的的TimerTask()类,你指的是一个变量,它的出类的范围。因为的TimerTask()不会从 ContextWrapper 子类,它不直接获取上下文。为了让活动(希望而不是的上下文应用!),你应该做的背景:上下文的背景下= ParsedExampleDataSet.this.getBaseContext( ); 这样,你可能不应该得到任何空指针异常

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

解决方案

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.

这篇关于找对象了一个方法(可运行)定时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆