2013-08-26 228 views
-1
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是越來越java.lang.ArrayIndexOutOfBoundsException

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:長度= 1; index = 1

PS。

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

+1

後整個logcat中也。 – GrIsHu

+0

哪條線是79線? –

+0

線79:valueTV.setText(adob [0]的ToString()+ 「[」 + adob [1]的ToString() \t \t \t \t \t \t + 「]」); –

回答

0

把這個代碼在postExecute PrevAlb的()()的AsyncTask而在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); 
     } 
    } 
+0

E/AndroidRuntime(21480):\t at com.app.pixitch.Albums $ PrevAb $ 1.onClick(Albums.java:180) E/AndroidRuntime(21480):\t at android.view.View.performClick(View.java :3511) E/AndroidRuntime(21480):\t at android.view.View $ PerformClick.run(View.java:14109) –

+0

This above Error is Coming ,,,, –

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

Exception告訴你一切

adob已經length=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() + "]"); 
} 
+0

if(!ares [0] .toString( ).equals(「0」))我只檢查第一..我陣列爲空我只在索引0中添加0 –

+0

@VireshNY它很好,它不是空的,我檢查null使它更可靠!但在這裏'adob = ares [i] .toString()。split(「,」);'你只是一個索引爲0的值!這就是爲什麼你得到例外 –