Android应用程序中的java.lang.StringIndexOutOfBoundsException

时间:2014-03-08 11:44:35

标签: java android indexoutofboundsexception

我的应用程序昨天在我的手机上崩溃了,谷歌开发者的错误如下:

java.lang.StringIndexOutOfBoundsException: length=69; regionStart=21; regionLength=-20 
at java.lang.String.startEndAndLength(String.java:593)   
at java.lang.String.substring(String.java:1474)
at com.somjah.radio.RadioService.getTrackFromAAC(RadioService.java:998)
at com.somjah.radio.RadioService.access$10(RadioService.java:985) 
at com.somjah.radio.RadioService$1.playerMetadata(RadioService.java:953)
at com.spoledge.aacdecoder.IcyInputStream.parseMetadata(IcyInputStream.java:231)
at com.spoledge.aacdecoder.IcyInputStream.fetchMetadata(IcyInputStream.java:205)  
at com.spoledge.aacdecoder.IcyInputStream.read(IcyInputStream.java:133)
at com.spoledge.aacdecoder.BufferReader.run(BufferReader.java:155)  
at java.lang.Thread.run(Thread.java:856)

这是RadioService.java的一部分:

@Override

public void playerMetadata(String key, String value) {

System.out.println("Metadata--------------");

System.out.println(key + " ==> " + value);

            if ("StreamTitle".equals(key) || "icy-name".equals(key)

|| "icy-description".equals(key)) {
            final String meta_artist = getArtistFromAAC(value);

            final String meta_track = getTrackFromAAC(value);

            handler.post(new Runnable() {

@Override
            public void run() {

            updateMetadataTitle(meta_artist, meta_track);

            }

            });


}

}

@Override

public void playerAudioTrackCreated(AudioTrack arg0) {

// TODO Auto-generated method stub

        }

};

    private String getArtistFromAAC(String streamTitle) {

    int end = streamTitle.indexOf("-");

    if (end <= 0)

    end = streamTitle.indexOf(":");

        String title;

        if (end > 0)

        title = streamTitle.substring(0, end);

    else

            title = streamTitle;

        return title.trim();

    }


private String getTrackFromAAC(String streamTitle) {

        int start = streamTitle.indexOf("-") + 1;

        if (start <= 0)

        start = streamTitle.indexOf(":") + 1;


String track;

        if (start > 0)

        track = streamTitle.substring(start);

        else

        track = streamTitle;

        int end = streamTitle.indexOf("(");

        if (end > 0)

        track = streamTitle.substring(start, end); //this is line 998

        end = streamTitle.indexOf("[");

        if (end > 0)

        track = streamTitle.substring(start, end);

        return track.trim();

    }


}

我认为错误在最后几行,但我无法弄清楚。缺少什么?

1 个答案:

答案 0 :(得分:0)

从堆栈跟踪中我们可以看到异常来自方法getTrackFromAAC()第998行,您调用substring()。该异常表示您正在传递substring()无效参数。 This link提供有关subString()的有效参数的信息。

你可能会收到这个异常,因为你传入一个字符串,其中“(”出现在“ - ”之前。你可以尝试将你的最终if语句改为

if (end > 0 && start < end)

以避免发生此异常