android AsyncTask xml解析

发布于 2024-11-04 06:42:20 字数 2967 浏览 4 评论 0原文

我尝试在 AsyncTask 中解析一个大的 xml 文档。 FeinstaubActivity 启动,但我只看到黑屏,然后返回到 RSSReaderActivity,从我启动另一个 Activity 的地方开始。

日志:

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

}

问候 漂浮

i try to parse a large xml document in a AsyncTask. The FeinstaubActivity starts but i only see a black screen and then get back to the RSSReaderActivity from where i started the other Activity.

Log:

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

Code

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

}

Regards
float

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乱世争霸 2024-11-11 06:42:21

啊,不确定你能做到这一点: http://developer.android.com/参考/android/os/AsyncTask.html显示execute返回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 内部回调它,但类似的事情应该做它。

Ah, not sure you can do that: http://developer.android.com/reference/android/os/AsyncTask.html shows that execute returns void.

You want this:

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

}

There's a bit of scoping you need to do: pass the parent activity in or declare the setDocument method appropriately somehow so you can call it back from inside the AsyncTask, but something like that should do it.

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