0

您好,我从事这个项目已经有一段时间了,目前非常卡住,我还是 Android 新手,并且一直在开发我的第一个应用程序,该应用程序从我使用 MediaWiki API 创建的 XML 文件中提取信息。每次我尝试在我的应用程序上选择“播放器”选项卡时,它都会强制关闭,我不知道为什么。这是我的玩家活动和 Wikiparser 活动的代码:

package com.lvlup.kikurself.scotttest;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import java.io.IOException;
import org.xml.sax.SAXException;

//import com.lvlup.kikurself.scotttest.WikiParser.Cm;

public class scottPlayers extends ListActivity {
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

WikiParser p = new WikiParser();
ArrayList<String> titles = new ArrayList<String>();

try {
    p.parseInto(new URL("http://scottlandminecraft.wikia.com/api.php?action=query&list=categorymembers&cmtitle=Category:Players&cmlimit=500&format=xml"), titles);

} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (SAXException e) {}

    //String[] values = new String[50]; 
    //values = res;




    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, titles);

    setListAdapter(adapter);

    final ListView playersList = getListView();


    playersList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long thisID)
    {
         Object o = (playersList.getItemAtPosition(position));
         String playerName_temp = (o.toString());

         Intent newIntent = new Intent(v.getContext(), playerDisp.class);
         newIntent.putExtra("tempN", playerName_temp);
         startActivity(newIntent);

    }
    });



    //get your data back again with: String fName = getIntent().getExtras().getInt("fname");




}}

接下来是 WikiParser 函数的代码:

package com.lvlup.kikurself.scotttest;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.sax.*;
import android.text.TextUtils;
import android.util.Xml;
import android.util.Xml.Encoding;

public class WikiParser {
private static class CmListener implements StartElementListener {
    final List<String> mTitles;
    CmListener(List<String> titles) {
        mTitles = titles;
    }
    @Override
    public void start(Attributes attributes) {
        String title = attributes.getValue("", "title");
        if (!TextUtils.isEmpty(title)) {
            mTitles.add(title);
        }
    }
}
public void parseInto(URL url, List<String> titles) throws IOException, SAXException {
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    try {
        parseInto(new BufferedInputStream(con.getInputStream()), titles);
    } finally {
        con.disconnect();
    }
}
public void parseInto(InputStream docStream, List<String> titles) throws IOException, SAXException {
    RootElement api = new RootElement("api");
    Element query = api.requireChild("query");
    Element categoryMembers = query.requireChild("categorymembers");
    Element cm = categoryMembers.requireChild("cm");
    cm.setStartElementListener(new CmListener(titles));
    Xml.parse(docStream, Encoding.UTF_8, api.getContentHandler());
}
}

就是这样......我在这里学习我的错误,所以任何人都可以告诉我我的代码以及我可以做些什么来改进,如果有人知道出了什么问题,提前谢谢你。

编辑:也包括 LogCat,谢谢。

    08-06 04:33:58.837: D/dalvikvm(539): GC_FOR_ALLOC freed 62K, 3% free 9883K/10179K, paused 64ms
08-06 04:33:58.917: I/dalvikvm-heap(539): Grow heap (frag case) to 13.641MB for 4096016-byte allocation
08-06 04:33:59.037: D/dalvikvm(539): GC_CONCURRENT freed <1K, 3% free 13882K/14215K, paused 12ms+17ms
08-06 04:33:59.748: D/gralloc_goldfish(539): Emulator without GPU emulation detected.
08-06 04:34:05.977: D/AndroidRuntime(539): Shutting down VM
08-06 04:34:05.987: W/dalvikvm(539): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
08-06 04:34:06.067: E/AndroidRuntime(539): FATAL EXCEPTION: main
08-06 04:34:06.067: E/AndroidRuntime(539): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lvlup.kikurself.scotttest/com.lvlup.kikurself.scotttest.scottPlayers}: android.os.NetworkOnMainThreadException
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.os.Looper.loop(Looper.java:137)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.app.ActivityThread.main(ActivityThread.java:4340)
08-06 04:34:06.067: E/AndroidRuntime(539):  at java.lang.reflect.Method.invokeNative(Native Method)
08-06 04:34:06.067: E/AndroidRuntime(539):  at java.lang.reflect.Method.invoke(Method.java:511)
08-06 04:34:06.067: E/AndroidRuntime(539):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-06 04:34:06.067: E/AndroidRuntime(539):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-06 04:34:06.067: E/AndroidRuntime(539):  at dalvik.system.NativeStart.main(Native Method)
08-06 04:34:06.067: E/AndroidRuntime(539): Caused by: android.os.NetworkOnMainThreadException
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
08-06 04:34:06.067: E/AndroidRuntime(539):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
08-06 04:34:06.067: E/AndroidRuntime(539):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
08-06 04:34:06.067: E/AndroidRuntime(539):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
08-06 04:34:06.067: E/AndroidRuntime(539):  at com.lvlup.kikurself.scotttest.WikiParser.parseInto(WikiParser.java:35)
08-06 04:34:06.067: E/AndroidRuntime(539):  at com.lvlup.kikurself.scotttest.scottPlayers.onCreate(scottPlayers.java:29)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.app.Activity.performCreate(Activity.java:4465)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
08-06 04:34:06.067: E/AndroidRuntime(539):  ... 11 more
4

2 回答 2

2

NetworkOnMainThreadException

当应用程序尝试在其主线程上执行网络操作时引发的异常。

这仅针对面向 Honeycomb SDK 或更高版本的应用程序抛出。允许以早期 SDK 版本为目标的应用程序在其主事件循环线程上进行网络连接,但非常不鼓励这样做。请参阅文档设计响应性。

另请参阅严格模式。

查看 Stackoverflow 上的现有答案 -

  1. NetworkOnMainThreadException

  2. android.os.NetworkOnMainThreadException

有关更多详细信息,您可以到这里

于 2012-08-06T04:45:06.073 回答
1
  1. 始终建议让 UI 在 UI 线程上工作,而 Non-UI 在 Non-UI 线程上工作,但这变成了了一种法则。

  2. android 中的应用程序从专用 UI​​ 线程开始,因此当您创建另一个线程来处理网络、fileIO 等长进程工作时,您将放弃专用 UI​​ 线程。

  3. 要同步 UI 线程和 Non-UI 线程,请使用Thread 与 Handler 或 AsyncTask,这是 android 中专门引入的,可以轻松实现 UI 和 Non-UI 线程之间的同步,也称为Painless Threading ....

于 2012-08-06T04:56:48.563 回答