0

我正在使用 sax 解析器来获取 xml 数据,下面的代码导致我的应用程序崩溃。

    try {
        url = new URL("The URL");
    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
   // RssFeed feed = null;
    try {
        feed = RssReader.read(url);
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

看起来它正在崩溃feed = RssReader.read(url);它在我的 evo 上运行良好,但是当我将它安装在我的英雄上时,会发生以下错误

04-28 15:57:53.458: E/AndroidRuntime(2749): Uncaught handler: thread main exiting due to uncaught  exception
04-28 15:57:53.589: E/AndroidRuntime(2749): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mlgw.mlgwapp/com.mlgw.mlgwapp.TipsActivity}: java.lang.StringIndexOutOfBoundsException
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2411)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:648)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.widget.TabHost.setCurrentTab(TabHost.java:320)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:379)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.view.View.performClick(View.java:2364)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.view.View.onTouchEvent(View.java:4179)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.view.View.dispatchTouchEvent(View.java:3709)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:874)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1695)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.app.Activity.dispatchTouchEvent(Activity.java:2068)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1679)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1697)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.os.Looper.loop(Looper.java:123)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.app.ActivityThread.main(ActivityThread.java:4595)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at java.lang.reflect.Method.invokeNative(Native Method)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at java.lang.reflect.Method.invoke(Method.java:521)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at dalvik.system.NativeStart.main(Native Method)
04-28 15:57:53.589: E/AndroidRuntime(2749): Caused by: java.lang.StringIndexOutOfBoundsException
04-28 15:57:53.589: E/AndroidRuntime(2749):     at java.lang.String.substring(String.java:1646)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at nl.matshofman.saxrssreader.RssHandler.endElement(RssHandler.java:67)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at org.apache.harmony.xml.ExpatParser.endElement(ExpatParser.java:159)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at org.apache.harmony.xml.ExpatParser.append(Native Method)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:506)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:467)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:329)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:286)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at nl.matshofman.saxrssreader.RssReader.read(RssReader.java:43)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at com.mlgw.mlgwapp.TipsActivity.onCreate(TipsActivity.java:57)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-28 15:57:53.589: E/AndroidRuntime(2749):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)

请帮忙!!

这是我的 RssHandler 类

package nl.matshofman.saxrssreader;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

public class RssHandler extends DefaultHandler {

private RssFeed rssFeed;
private RssItem rssItem;
private StringBuilder stringBuilder;

@Override
public void startDocument() {
    rssFeed = new RssFeed();
}

/**
 * Return the parsed RssFeed with it's RssItems
 * @return
 */
public RssFeed getResult() {
    return rssFeed;
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
    stringBuilder = new StringBuilder();

    if(qName.equals("item") && rssFeed != null) {
        rssItem = new RssItem();
        rssItem.setFeed(rssFeed);
        rssFeed.addRssItem(rssItem);
    }
}

@Override
public void characters(char[] ch, int start, int length) {
    stringBuilder.append(ch, start, length);
}

@Override
public void endElement(String uri, String localName, String qName) {

    if(rssFeed != null && rssItem == null) {
        // Parse feed properties

        try {
            String methodName = "set" + qName.substring(0, 1).toUpperCase() + qName.substring(1);
            Method method = rssFeed.getClass().getMethod(methodName, String.class);
            method.invoke(rssFeed, stringBuilder.toString());
        } catch (SecurityException e) {
        } catch (NoSuchMethodException e) {
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        }

    } else if (rssItem != null) {
        // Parse item properties

        try {
            if(qName.equals("content:encoded")) 
                qName = "content";
            String methodName = "set" + qName.substring(0, 1).toUpperCase() + qName.substring(1);
            Method method = rssItem.getClass().getMethod(methodName, String.class);
            method.invoke(rssItem, stringBuilder.toString());
        } catch (SecurityException e) {
        } catch (NoSuchMethodException e) {
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        }
    }

}

好的,我补充说现在有这个

04-28 18:49:35.189: E/AndroidRuntime(3780): Uncaught handler: thread main exiting due to uncaught exception
04-28 18:49:35.328: E/AndroidRuntime(3780): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mlgw.mlgwapp/com.mlgw.mlgwapp.TipsActivity}: java.lang.IllegalArgumentException
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2411)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:648)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.widget.TabHost.setCurrentTab(TabHost.java:320)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:379)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.view.View.performClick(View.java:2364)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.view.View.onTouchEvent(View.java:4179)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.view.View.dispatchTouchEvent(View.java:3709)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:874)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1695)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.app.Activity.dispatchTouchEvent(Activity.java:2068)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1679)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1697)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.os.Looper.loop(Looper.java:123)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.app.ActivityThread.main(ActivityThread.java:4595)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at java.lang.reflect.Method.invokeNative(Native Method)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at java.lang.reflect.Method.invoke(Method.java:521)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at dalvik.system.NativeStart.main(Native Method)
04-28 18:49:35.328: E/AndroidRuntime(3780): Caused by: java.lang.IllegalArgumentException
04-28 18:49:35.328: E/AndroidRuntime(3780):     at java.util.Random.nextInt(Random.java:221)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at com.mlgw.mlgwapp.TipsActivity.onCreate(TipsActivity.java:71)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-28 18:49:35.328: E/AndroidRuntime(3780):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
04-28 18:49:35.328: E/AndroidRuntime(3780):     ... 30 more

现在似乎与我正在使用的圆形功能有关。

4

1 回答 1

1

堆栈跟踪会告诉您错误的原因:

Caused by: java.lang.StringIndexOutOfBoundsException
     at java.lang.String.substring(String.java:1646)
     at nl.matshofman.saxrssreader.RssHandler.endElement(RssHandler.java:67)

实际错误发生在java.lang.String.substring()您的代码之外,因此您无法修复任何内容。但下一行告诉你在哪里substring被调用。RssHandler.java在第 67 行,在方法中endElement

那应该是:

String methodName = "set" + qName.substring(0, 1).toUpperCase() + qName.substring(1)

字符串索引是它们的字符,因此您可以访问qName它,尽管它是空的,并且您不能仅从第一个字符或从第一个字符开始构建子字符串。

为了防止崩溃,你可以做

if (qName != null && qName.length() > 0) {
    String methodName = "set" + qName.substring(0, 1).toUpperCase() + qName.substring(1);
    Method method = rssFeed.getClass().getMethod(methodName, String.class);
    method.invoke(rssFeed, stringBuilder.toString());
}

您应该查看 XML 并找出为什么存在不符合预期格式的 qName。

localName可能更好地检查,因为 is 的限定名称 <xsl:template />xsl:template本地名称是 just template

于 2012-04-28T21:40:55.940 回答