获取java.lang.ArrayIndexOutOfBoundsException

时间:2013-08-26 09:12:48

标签: java android

    ares = null;
    adob = null;
    ares = tres.split("-");
    if (!ares[0].toString().equals("0")) {
        for (int i = 0; i <= ares.length - 1; i++) {
            adob = ares[i].toString().split(",");
            // txtv = new TextView(Albums.this);
            valueTV = new TextView(Albums.this);
            valueTV.setText(adob[0].toString() + "[" + adob[1].toString()
                    + "]"); // Here I am Getting Exception !
            // valueTV.setId(i);
            valueTV.setGravity(Gravity.CENTER);
            valueTV.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            valueTV.setTextSize(20);
            valueTV.setTextColor(Color.BLUE);
            valueTV.setClickable(true);

错误LogCat

08-26 14:35:00.945: W/dalvikvm(21180): threadid=1: thread exiting with uncaught exception (group=0x409e61f8)
08-26 14:35:00.975: D/dalvikvm(21180): GC_CONCURRENT freed 218K, 4% free 6798K/7047K, paused 2ms+3ms
08-26 14:35:00.975: E/AndroidRuntime(21180): FATAL EXCEPTION: main
08-26 14:35:00.975: E/AndroidRuntime(21180): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.pixitch/com.app.pixitch.Albums}: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
08-26 14:35:00.975: E/AndroidRuntime(21180):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-26 14:35:00.975: E/AndroidRuntime(21180):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-26 14:35:00.975: E/AndroidRuntime(21180):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-26 14:35:00.975: E/AndroidRuntime(21180):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-26 14:35:00.975: E/AndroidRuntime(21180):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 14:35:00.975: E/AndroidRuntime(21180):    at android.os.Looper.loop(Looper.java:137)
08-26 14:35:00.975: E/AndroidRuntime(21180):    at android.app.ActivityThread.main(ActivityThread.java:4424)
08-26 14:35:00.975: E/AndroidRuntime(21180):    at java.lang.reflect.Method.invokeNative(Native Method)
08-26 14:35:00.975: E/AndroidRuntime(21180):    at java.lang.reflect.Method.invoke(Method.java:511)
08-26 14:35:00.975: E/AndroidRuntime(21180):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-26 14:35:00.975: E/AndroidRuntime(21180):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-26 14:35:00.975: E/AndroidRuntime(21180):    at dalvik.system.NativeStart.main(Native Method)
08-26 14:35:00.975: E/AndroidRuntime(21180): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
08-26 14:35:00.975: E/AndroidRuntime(21180):    at com.app.pixitch.Albums.onCreate(Albums.java:79)
08-26 14:35:00.975: E/AndroidRuntime(21180):    at android.app.Activity.performCreate(Activity.java:4492)
08-26 14:35:00.975: E/AndroidRuntime(21180):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-26 14:35:00.975: E/AndroidRuntime(21180):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-26 14:35:00.975: E/AndroidRuntime(21180):    ... 11 more

当我逐步调试程序时,它工作得很好。当我在没有调试的情况下运行程序时,它会给出错误,如

  

无法开始活动   ComponentInfo {com.app.pixitch / com.app.pixitch.Albums}:   java.lang.ArrayIndexOutOfBoundsException:length = 1;索引= 1

PS。

第79行:valueTV.setText(adob[0].toString() + "[" + adob[1].toString() + "]");

2 个答案:

答案 0 :(得分:0)

将此代码放在PrevAlb()asynctask的postExecute()中,而不是放在onCreate()方法中。

ares = tres.split("-");
    if (!ares[0].toString().equals("0")) {
        for (int i = 0; i <= ares.length - 1; i++) {
            adob = ares[i].toString().split(",");
            // txtv = new TextView(Albums.this);
            valueTV = new TextView(Albums.this);
            valueTV.setText(adob[0].toString() + "[" + adob[1].toString()
                    + "]");
            // valueTV.setId(i);
            valueTV.setGravity(Gravity.CENTER);
            valueTV.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            valueTV.setTextSize(20);
            valueTV.setTextColor(Color.BLUE);
            valueTV.setClickable(true);
            valueTV.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    imgArrayList = null;
                    new GetAlb(adob[2].toString().trim()).execute();
                    if (!strimgurls[0].toString().equals("0")) {
                        for (int j = 0; j <= strimgurls.length - 1; j++) {
                            String[] tmpimgurls = strimgurls[j].toString()
                                    .split("_");
                            String extn = strimgurls[j]
                                    .toString()
                                    .trim()
                                    .substring(
                                            strimgurls[j].toString().trim()
                                                    .lastIndexOf("."));
                            String fname = strimgurls[j].toString().trim()
                                    .replace(extn, "_b" + extn);
                            String temp = methods.albimg.trim()
                                    + tmpimgurls[1].toString() + "/"
                                    + tmpimgurls[2].toString() + "/"
                                    + tmpimgurls[3].toString() + "/"
                                    + fname.trim();
                            imgArrayList.add(temp.trim());
                        }
                        String[] imgulls = imgArrayList
                                .toArray(new String[imgArrayList.size()]);
                        Intent i = new Intent(Albums.this,
                                ImageGridActivity.class);
                        i.putExtra("imgurls", imgulls);
                        startActivity(i);
                    }
                }
            });
            lnrLayout.addView(valueTV);
        }
    }

答案 1 :(得分:0)

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

你的Exception告诉你一切

您的adoblength=1,即只有index 0可以访问adob[0]并且您正在尝试访问不存在的index 1 adob[1]

更改

 valueTV.setText(adob[0].toString() + "[" + adob[1].toString() + "]");

if(adob!=null && adob.length>=2)
{
      valueTV.setText(adob[0].toString() + "[" + adob[1].toString() + "]");
}