2010-08-11 66 views
1

我已經實現了一個計時器,每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); 
+1

瘋狂的嵌套縮進,爲了清晰起見,嘗試將某些代碼從onCreate移出。 – LordTwaroog 2010-08-11 15:50:43

回答

1

我想這是因爲當你這樣做:Context context = this.getBaseContext()TimerTask()類中,你指的是一個變量,它是出類的範圍。由於TimerTask()未從ContextWrapper繼承,因此它不直接獲取上下文。要獲得活動的上下文(希望!而不是Application的上下文),你應該這樣做:Context context = ParsedExampleDataSet.this.getBaseContext();這樣你可能不應該得到任何空指針異常。