2011-04-27 67 views
0

我嘗試解析AsyncTask中的大型XML文檔。 FeinstaubActivity啓動,但我只看到一個黑屏,然後從我開始其他活動的位置返回到RSSReaderActivity。android AsyncTask xml解析

登錄:

DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol 
WARN/KeyCharacterMap(623): No keyboard for id 0 
WARN/KeyCharacterMap(623): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
INFO/ActivityManager(58): Starting activity: Intent { cmp=de.test.testapp/.FeinstaubActivity } 
DEBUG/AndroidRuntime(623): Shutting down VM 
WARN/dalvikvm(623): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
WARN/dalvikvm(623): threadid=7: thread exiting with uncaught exception (group=0x4001d800) 
INFO/Process(623): Sending signal. PID: 623 SIG: 9 
INFO/ActivityManager(58): Process de.test.testapp (pid 623) has died. 
INFO/WindowManager(58): WIN DEATH: Window{4402be18 AtchDlg:de.test.testapp/de.test.testapp.RSSReaderActivity paused=false} 
INFO/WindowManager(58): WIN DEATH: Window{4400dd68 de.test.testapp/de.test.testapp.RSSReaderActivity paused=false} 
INFO/ActivityManager(58): Start proc de.test.testapp for activity de.test.testapp/.RSSReaderActivity: pid=631 uid=10035 gids={3003} 
DEBUG/dalvikvm(32): GC_EXPLICIT freed 299 objects/11488 bytes in 162ms 
INFO/UsageStats(58): Unexpected resume of de.test.testapp while already resumed in de.test.testapp 
DEBUG/dalvikvm(32): GC_EXPLICIT freed 57 objects/2440 bytes in 138ms 
INFO/ActivityManager(58): Displayed activity de.test.testapp/.RSSReaderActivity: 411 ms (total 532 ms) 
DEBUG/dalvikvm(32): GC_EXPLICIT freed 2 objects/48 bytes in 126ms 
DEBUG/dalvikvm(631): GC_FOR_MALLOC freed 3294 objects/461928 bytes in 72ms 
DEBUG/dalvikvm(631): GC_FOR_MALLOC freed 2750 objects/557024 bytes in 59ms 
DEBUG/dalvikvm(631): GC_FOR_MALLOC freed 2786 objects/506288 bytes in 60ms 
WARN/InputManagerService(58): Got RemoteException sending setActive(false) notification to pid 623 uid 10035 

代碼

Document document = null; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.feinstaub); 
      document = (Doxument) new ParseXMLFile().execute(); 
    } 

    private class ParseXMLFile extends AsyncTask<Integer, Integer, Document>{ 

    @Override 
    protected Document doInBackground(Integer... params) { 
     Document parsedXML = null; 
     try { 
      parsedXML = builder.parse(getApplicationContext().getResources().openRawResource(R.raw.finedust)); 
     } catch (NotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (SAXException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return parsedXML; 
    } 

} 

問候 浮動

回答

1

啊,不知道你能做到這一點:http://developer.android.com/reference/android/os/AsyncTask.html顯示,執行返回void。

你想這樣的:

Document document = null; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.feinstaub); 
     new ParseXMLFile().execute(); 
    } 

    void setDocument(Document document){ this.document = document; } 

    private class ParseXMLFile extends AsyncTask<Integer, Integer, Document>{ 
    @Override public void onPostExecute(Document d){ setDocument(d); } 

    @Override 
    protected Document doInBackground(Integer... params) { 
     Document parsedXML = null; 
     try { 
      parsedXML = builder.parse(getApplicationContext().getResources().openRawResource(R.raw.finedust)); 
     } catch (NotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (SAXException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return parsedXML; 
    } 

} 

有一點確定範圍的,你需要做的:通過在父活動或聲明setDocument方法適當不知何故,所以你可以從的AsyncTask裏面打電話回來,但一些像那樣應該這樣做。

+0

謝謝,這個作品完美! – float 2011-04-28 06:23:09

+0

你也知道爲什麼Button.setClickable(false);被忽略,我可以點擊按鈕? – float 2011-04-28 08:11:31

+0

這是在AsyncTask? – Femi 2011-04-28 08:17:22