user3864563
user3864563

Reputation: 395

App crashes when trying to access array Android SDK

So, here is the file I am trying to interpret in a thread

% $- background:#000000 % Hello, World. It is currently a test of the features of YeML. % $- background:#ffffff % $+ id:test type:container

Here is the code:

new Thread() {
        StringBuilder text = new StringBuilder();
        @Override
        public void run() {
            try

            {
                String str = "";
                URL url = new URL("http://example.com/" + mes + "/" + mes + ".meb");
                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

                while ((str = in.readLine()) != null) {
                    text.append(str);
                }
                in.close();
            } catch (MalformedURLException e1)

            {
            }
            catch (IOException e)
            {
            }
            if(message.contains(".meb")) {
                String str[] = text.toString().split("%");
                for (final String l : str) {
                    String code[] = l.split(" ");
                    if (l.toString().contains("$-")) {
                        for(String p : code)
                        {
                            if(p.toString().contains("background"))
                            {
                                final String set[] = p.toString().split(":");
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                (findViewById(R.id.body)).setBackgroundColor(Color.parseColor(set[1]));
                                    }
                                });
                            }
                        }
                    }
                    else if(l.toString().contains("$+"))
                    {
                        String[] g = code[1].split(":");
                        String[] c = code[2].split(":");
                        final String[] test = {g[1], "100"};
                        globvar.add(test);
                        if(test[1].toString().equals("container")) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    final LinearLayout element = new LinearLayout(getApplicationContext());
                                    element.setId(Integer.parseInt(test[1]));
                                    element.setBackgroundColor(Color.parseColor("#000000"));
                                    ((LinearLayout) findViewById(R.id.linearo)).addView(element);
                                }
                            });
                        }
                    }
                    else
                    {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    final TextView tv = new TextView(getApplicationContext());
                                    tv.setTextColor(Color.parseColor("#000000"));
                                    tv.setText(l);
                                    ((LinearLayout) findViewById(R.id.linearo)).addView(tv);
                                }
                            });
                    }
                }
            }
            else {
                if(message == null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                        }
                    });
                }
            }
        }
    }.start();

I know it occurs at the part final String[] test = {g[1], "100"}; because here is the logcat:

08-01 17:05:36. 186 20045 20065 E AndroidRuntime: FATAL EXCEPTION: Thread-1111
08-01 17:05:36. 186 20045 20065 E AndroidRuntime: java.lang.ArrayIndexOutOfBoundsException: length=1 ; index=1
08-01 17:05:36. 186 20045 20065 E AndroidRuntime: at example.yemeb.List$1.run(List.java:86)

And the line of code given before is at line 86. I don't know why it would be an array exception since the string array should consist of the following:

id:test -> id test

There should be an array of index 0, which is id, and an array index of 1, which is test. What is the problem? Thanks for any help.

Upvotes: 0

Views: 69

Answers (2)

Mikel Pascual
Mikel Pascual

Reputation: 2211

Logcat is being very clear here. It is tellíng you that you are trying to access g[1], but g[]`` only has one element. So, you only have g[0].

In your code, you never access any [0] index. Maybe you are thinking that an array in java starts by the element 1. In java, arrays start at element 0.

Upvotes: 0

reidisaki
reidisaki

Reputation: 1524

well i think the issue is that it says Array length is 1 and the index is 1.. but the index should be 0? java indices start at 0.

Try and check your array length in the debugger or output a logcat.

Upvotes: 1

Related Questions