引起:java.lang.ArrayIndexOutofBoundsException:index = 1

时间:2014-08-24 23:28:45

标签: java android android-studio

我一直在使用此应用程序从网页上下载所有图像大约一天,在谷歌约5小时后,没有答案出现。我不断收到标题中的错误消息以及以下logcat错误。

java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:864)
 Caused by: java.lang.ArrayIndexOutOfBoundsException: index=1
        at java.util.regex.Matcher.appendEvaluated(Matcher.java:149)
        at java.util.regex.Matcher.appendReplacement(Matcher.java:111)
        at java.util.regex.Matcher.replaceAll(Matcher.java:321)
        at java.lang.String.replaceAll(String.java:1785)
        at com.luke.test.Download$Main.getImages(Download.java:117)
        at com.luke.test.Download$Main.doInBackground(Download.java:91)
        at com.luke.test.Download$Main.doInBackground(Download.java:74)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)

除了我提供的错误外,我也有我的代码。

public class Download extends Activity {
    ProgressDialog mProgressDialog;
    EditText editText;
    Button button;
    String data;
    private String URL1;
    private ImageView downloadedImg;
    private ProgressDialog simpleWaitDialog;
    String stored = null;
    private String folderPath = android.os.Build.MODEL + "\\Internal storage\\Pictures\\Screenshots\\";
    private static final String name = ".jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download);
        editText = (EditText) findViewById(R.id.editText1);
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                data = editText.getText().toString();
                URL1 = data;
                Log.v("URL", URL1);
                new Main().execute();
            }
        });
    }

    private class Main extends AsyncTask<Void, Void, Void> {
        public Void doInBackground(Void... params) {

            try {


                Document doc = Jsoup.connect(URL1).get();

                Elements img = doc.getElementsByTag("img");

                for (Element el : img) {


                    String src = el.absUrl("src");
                    System.out.println("Image Found!");
                    System.out.println("src attribute is : " + src);

                    getImages(src, folderPath, name);

                }

            } catch (IOException ex) {
                System.err.println("There was an error");
                Logger.getLogger(Download.class.getName()).log(Level.SEVERE, null, ex);
            }

            return null;
        }

        private void getImages(String src, String folderPath, String name) throws IOException {

            int indexname = src.lastIndexOf("/");

            if (indexname == src.length()) {
                src = src.substring(1, indexname);
            }

            indexname = src.lastIndexOf("/");
            String srcname = src.substring(indexname, src.length());
            String Path = folderPath;

            String myString = Path + srcname + name;
            myString = myString.replace("?", "");
            myString = myString.replaceAll("/", "\\");
            System.out.println(myString);

            URL url = new URL(src);
            InputStream in = new BufferedInputStream(url.openStream());
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int n = 0;
            while (-1!=(n=in.read(buf)))
            {
                out.write(buf, 0, n);
            }
            out.close();
            in.close();
            byte[] response = out.toByteArray();
            FileOutputStream fos = new FileOutputStream(myString);
            fos.write(response);
            fos.close();
        }

        protected void OnPostExecute(Void result) {
            mProgressDialog.dismiss();
        }
    }
}

我知道,它非常笨重,而且我目前没有使用的字符串非常过多,这可能是我问题的一部分。所以,尽管如此,我们将非常感谢能够帮助您解决这个错误。

3 个答案:

答案 0 :(得分:2)

stacktrace告诉你问题出在这一行:

myString = myString.replaceAll("/", "\\");

这两个字符串用作正则表达式。这样做但没有正则表达式:

myString = myString.replace("/", "\\");

答案 1 :(得分:1)

好吧,我设法用一个非常简单的应用程序重新创建你的异常:

public class Test {

     public static void main(String []args){
        String myString = "a/b";
        myString = myString.replaceAll("/", "\\");
        System.out.println(myString);
     }
}

此代码给出:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.charAt(String.java:658)
    at java.util.regex.Matcher.appendReplacement(Matcher.java:762)
    at java.util.regex.Matcher.replaceAll(Matcher.java:906)
    at java.lang.String.replaceAll(String.java:2162)
    at Test.main(Test.java:5)

replaceAll替换replace可以解决问题。

另一个有效的选择是 myString = myString.replaceAll("/", Matcher.quoteReplacement("\\"));

这会抑制替换字符串中\的特殊含义。

答案 2 :(得分:0)

另外两个答案正在引导你朝着正确的方向前进。这段代码:

int indexname = src.lastIndexOf("/");

if (indexname == src.length()) {
    src = src.substring(1, indexname);
}

看起来不对。 indexname永远不会等于src.length()。 lastIndexOf永远不能返回等于搜索字符串长度的值。在不匹配的情况下,它始终是[0,length-1]或-1之间的值。