【问题标题】:Get Object out of a method(runnable) timer从方法(可运行)计时器中获取对象
【发布时间】:2010-08-11 14:16:51
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 疯狂的嵌套缩进,为了清晰起见,尝试将一些代码移出 onCreate。

标签: java android bitmap timer runnable


【解决方案1】:

我认为这是因为当你这样做时:Context context = this.getBaseContext()TimerTask() 类中,你指的是一个超出类范围的变量。因为TimerTask() 不是ContextWrapper 的子类,所以它不会直接获取上下文。要获取活动的上下文(希望!而不是 Application 的上下文),您应该这样做:Context context = ParsedExampleDataSet.this.getBaseContext(); 这样您可能不应该得到任何空指针异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-04
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 2021-10-29
    相关资源
    最近更新 更多