sax解析器循环错误

发布于 2024-10-29 03:33:30 字数 5720 浏览 1 评论 0原文

我正在尝试使用 SAX 解析器来解析 HTTP 响应。一切似乎都工作正常,除了程序没有进入我的 for 循环。这是我的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.playerinfo);

    TableLayout t1 = (TableLayout) findViewById(R.id.myTable);

    Intent i = getIntent();
    Bundle b = i.getExtras();
    final String str = b.getString("ARRIVING_FROM");

    d=d+"";
    d = tryLogin(str);
    System.out.println("Value of D"+d.substring(0, 1)); 

    this.tryLogin(str);

    final ScoreList scorelist = XMLHandler.scorelist ;

    id = new TextView[scorelist.getName().size()];
    name = new TextView[scorelist.getName().size()];

    for (int j = 0; j < scorelist.getName().size(); j++) {
        TableRow tr = new TableRow(this);

        id[j]= new TextView(this);
        id[j].setText(scorelist.getId().get(j));
        id[j].setTextColor(Color.WHITE);
        id[j].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        tr.addView(id[j]);

        name[j]= new TextView(this);
        name[j].setText(scorelist.getName().get(j));
        name[j].setTextColor(Color.WHITE);
        name[j].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        tr.addView(name[j]);

        t1.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    }
}

protected String tryLogin(String str) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://ip address/test/players_detail.php?id="+str);
    List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
    nvps.add(new BasicNameValuePair("id", str));

    try  {
        UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
        httppost.setEntity(p_entity);
        HttpResponse response = client.execute(httppost);
        Log.v("MyPlayerInfo", response.getStatusLine().toString());
        HttpEntity responseEntity = response.getEntity();
        InputStream in=responseEntity.getContent();
        byte[] bData = new byte[1024];
        in.read(bData);
        System.out.println("In Data"+in.toString());
        String st=new String (bData);
        d=st;
        System.out.println("Response String from server"+st);
        Log.v("MyPlayerInfo", "Set response to responseEntity");

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        System.out.println("Response from server"+responseEntity);
        XMLHandler myXMLHandler = new XMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(retrieveInputStream(responseEntity));

        System.out.println("Server Response"+responseEntity);
        return d;
    } catch(Exception e) {
        Log.i("Catch","Exception generate in Post"+e);
        e.printStackTrace();
    }

    return "0";
}

private InputSource retrieveInputStream(HttpEntity httpEntity) {
    InputSource insrc = (InputSource) httpEntity;

    try {
        insrc = new InputSource(httpEntity.getContent());
    } catch (Exception e) {}

    return insrc;
}

logcat 响应是:

04-01 12:04:21.746: ERROR/AndroidRuntime(808): FATAL EXCEPTION: main
04-01 12:04:21.746: ERROR/AndroidRuntime(808): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.example/android.example.MyPlayerInfo}: java.lang.NullPointerException
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.os.Looper.loop(Looper.java:123)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at java.lang.reflect.Method.invokeNative(Native Method)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at java.lang.reflect.Method.invoke(Method.java:521)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at dalvik.system.NativeStart.main(Native Method)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): Caused by: java.lang.NullPointerException
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.example.MyPlayerInfo.onCreate(MyPlayerInfo.java:67)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-01 12:04:21.756: WARN/ActivityManager(59):   Force finishing activity android.example/.MyPlayerInfo
04-01 12:04:21.766: WARN/ActivityManager(59):   Force finishing activity android.example/.MyParsingExample
04-01 12:04:22.266: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{43ff6188 android.example/.MyPlayerInfo}

I'm trying to use a SAX parser to parse a HTTP response. Everything seems to be working correctly, except that the program isn't entering my for loop. Here is my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.playerinfo);

    TableLayout t1 = (TableLayout) findViewById(R.id.myTable);

    Intent i = getIntent();
    Bundle b = i.getExtras();
    final String str = b.getString("ARRIVING_FROM");

    d=d+"";
    d = tryLogin(str);
    System.out.println("Value of D"+d.substring(0, 1)); 

    this.tryLogin(str);

    final ScoreList scorelist = XMLHandler.scorelist ;

    id = new TextView[scorelist.getName().size()];
    name = new TextView[scorelist.getName().size()];

    for (int j = 0; j < scorelist.getName().size(); j++) {
        TableRow tr = new TableRow(this);

        id[j]= new TextView(this);
        id[j].setText(scorelist.getId().get(j));
        id[j].setTextColor(Color.WHITE);
        id[j].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        tr.addView(id[j]);

        name[j]= new TextView(this);
        name[j].setText(scorelist.getName().get(j));
        name[j].setTextColor(Color.WHITE);
        name[j].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        tr.addView(name[j]);

        t1.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    }
}

protected String tryLogin(String str) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://ip address/test/players_detail.php?id="+str);
    List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
    nvps.add(new BasicNameValuePair("id", str));

    try  {
        UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
        httppost.setEntity(p_entity);
        HttpResponse response = client.execute(httppost);
        Log.v("MyPlayerInfo", response.getStatusLine().toString());
        HttpEntity responseEntity = response.getEntity();
        InputStream in=responseEntity.getContent();
        byte[] bData = new byte[1024];
        in.read(bData);
        System.out.println("In Data"+in.toString());
        String st=new String (bData);
        d=st;
        System.out.println("Response String from server"+st);
        Log.v("MyPlayerInfo", "Set response to responseEntity");

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        System.out.println("Response from server"+responseEntity);
        XMLHandler myXMLHandler = new XMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(retrieveInputStream(responseEntity));

        System.out.println("Server Response"+responseEntity);
        return d;
    } catch(Exception e) {
        Log.i("Catch","Exception generate in Post"+e);
        e.printStackTrace();
    }

    return "0";
}

private InputSource retrieveInputStream(HttpEntity httpEntity) {
    InputSource insrc = (InputSource) httpEntity;

    try {
        insrc = new InputSource(httpEntity.getContent());
    } catch (Exception e) {}

    return insrc;
}

And the logcat response is:

04-01 12:04:21.746: ERROR/AndroidRuntime(808): FATAL EXCEPTION: main
04-01 12:04:21.746: ERROR/AndroidRuntime(808): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.example/android.example.MyPlayerInfo}: java.lang.NullPointerException
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.os.Looper.loop(Looper.java:123)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at java.lang.reflect.Method.invokeNative(Native Method)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at java.lang.reflect.Method.invoke(Method.java:521)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at dalvik.system.NativeStart.main(Native Method)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): Caused by: java.lang.NullPointerException
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.example.MyPlayerInfo.onCreate(MyPlayerInfo.java:67)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-01 12:04:21.756: WARN/ActivityManager(59):   Force finishing activity android.example/.MyPlayerInfo
04-01 12:04:21.766: WARN/ActivityManager(59):   Force finishing activity android.example/.MyParsingExample
04-01 12:04:22.266: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{43ff6188 android.example/.MyPlayerInfo}

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

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

发布评论

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

评论(1

川水往事 2024-11-05 03:33:30

好的,MyPlayerInfo.java 的第 67 行出现了 NullPointerException。查看该行,找出出现异常的原因,然后修复它。

如果不知道第 67 行是哪一行,则很难进一步提供帮助 - 您已经提供了大量代码来查看,并且我们不知道它在哪里损坏,但是您 应该。

(顺便说一句,您应该在finally块中关闭输入流,并且只捕获更具体的异常,但一次只捕获一件事......)

Okay, so you've got a NullPointerException at line 67 of MyPlayerInfo.java. Look at that line, work out why you're getting the exception, and fix it.

Without knowing which line is line 67, it's tricky to help further - you've presented a lot of code to look at, and we don't know where it's breaking, but you should.

(As an aside, you should be closing the input stream in a finally block, and only catching more specific exceptions, but one thing at a time...)

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