2014-02-20 62 views
0

我的活動,我從數據中獲取數據並將其設置爲帶有列的錶行,但我有錯誤是索引超出界限。可以解釋爲什麼是這樣以及如何做正確的解決方案。索引超出界限例外與android中的表格佈局

這是我的代碼

public class MybookActivity extends Activity{ 

private BooksDB db; 
private Context context; 
private HashMap< String, ArrayList<String>> hm; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.layout_mybook_sub2); 

    ArrayList<String> list_bokID = new ArrayList<String>(); 
    ArrayList<String> list_title = new ArrayList<String>(); 
    ArrayList<String> list_theme = new ArrayList<String>(); 
    hm = new HashMap<String, ArrayList<String>>(); 


    context = this; 
    db = new BooksDB(context); 
    hm = db.getBookTheme(); 
    if(hm.isEmpty() == true){ 
     System.out.println("NO data"); 

    }else{ 
     System.out.println("have data"); 
     list_bokID = hm.get("bokID"); 
     for (String bokID : list_bokID){ 
      System.out.println(bokID); 
     } 
     list_title = hm.get("title"); 
     for (String title : list_title) { 
      System.out.println(title); 

     } 
     list_theme = hm.get("theme"); 
     for (String themePath : list_theme) { 
      System.out.println(themePath); 
     } 
    } 

    int Theme_size = list_theme.size(); 
    //new 
    int numRow = Theme_size; 



    TableLayout tblLayout = (TableLayout) findViewById(R.id.tblLayout); 

    for(int i = 1; i < numRow; i++) { 
     HorizontalScrollView HSV = new HorizontalScrollView(this); 
     HSV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

     TableRow tblRow = new TableRow(this); 
     tblRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
     tblRow.setBackgroundResource(R.drawable.newshell); 

     for(int j = (i -1)*5; j < i*5; j++) { 

      Log.i("i",Integer.toString(i,j)); 
      Log.i("j", Integer.toString(j)); 
      ImageView imageView = new ImageView(this); 

       if (list_theme.size() > j){ 
        String themeName = list_theme.get(j); 
        res_id = getResources().getIdentifier(themeName, "drawable", getPackageName()); 
        imageView.setImageResource(res_id); 
        tblRow.addView(imageView,j); 
       } 

      } 


     HSV.addView(tblRow); 
     tblLayout.addView(HSV, i); 
    } 
} 

} 

這是我的logcat錯誤

02-20 23:15:03.656: I/i(17729): 1 
02-20 23:15:03.656: I/j(17729): 0 
02-20 23:15:03.681: I/i(17729): 1 
02-20 23:15:03.681: I/j(17729): 1 
02-20 23:15:03.706: I/i(17729): 1 
02-20 23:15:03.706: I/j(17729): 2 
02-20 23:15:03.711: I/i(17729): 1 
02-20 23:15:03.711: I/j(17729): 3 
02-20 23:15:03.711: I/i(17729): 1 
02-20 23:15:03.711: I/j(17729): 4 
02-20 23:15:03.731: D/dalvikvm(17729): GC_FOR_ALLOC freed 24K, 13% free 15656K/17991K, paused 10ms, total 10ms 
02-20 23:15:03.736: D/AndroidRuntime(17729): Shutting down VM 
02-20 23:15:03.736: W/dalvikvm(17729): threadid=1: thread exiting with uncaught exception (group=0x40f472a0) 
02-20 23:15:03.741: E/AndroidRuntime(17729): FATAL EXCEPTION: main 
02-20 23:15:03.741: E/AndroidRuntime(17729): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.splash.bookguk_project/category_viewbook_sub2_1.MybookActivity}: java.lang.IndexOutOfBoundsException: index=1 count=0 
02-20 23:15:03.741: E/AndroidRuntime(17729): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at android.app.ActivityThread.access$700(ActivityThread.java:140) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at android.os.Looper.loop(Looper.java:137) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at android.app.ActivityThread.main(ActivityThread.java:4921) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at java.lang.reflect.Method.invokeNative(Native Method) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at java.lang.reflect.Method.invoke(Method.java:511) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at dalvik.system.NativeStart.main(Native Method) 
02-20 23:15:03.741: E/AndroidRuntime(17729): Caused by: java.lang.IndexOutOfBoundsException: index=1 count=0 
02-20 23:15:03.741: E/AndroidRuntime(17729): at android.view.ViewGroup.addInArray(ViewGroup.java:3703) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at android.view.ViewGroup.addViewInner(ViewGroup.java:3642) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at android.view.ViewGroup.addView(ViewGroup.java:3491) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at android.widget.TableLayout.addView(TableLayout.java:425) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at android.view.ViewGroup.addView(ViewGroup.java:3436) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at android.widget.TableLayout.addView(TableLayout.java:407) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at category_viewbook_sub2_1.MybookActivity.onCreate(MybookActivity.java:99) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at android.app.Activity.performCreate(Activity.java:5206) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
02-20 23:15:03.741: E/AndroidRuntime(17729): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074) 
02-20 23:15:03.741: E/AndroidRuntime(17729): ... 11 more 

謝謝

回答

0

我想問題的行是:

for(int j = (i -1)*5; j < i*5; j++) 
    String themeName = list_theme.get(j); 

您正在訪問j中的項目j e數組,但你不檢查它的邊界。你只是假設它有很多項目。你需要添加像

if (list_theme.size >= j) 
    String themeName = list_theme.get(j); 

另一方面,我從1開始,這總是一個可疑的事情。 (我不是說錯了,只是奇怪)

for(int i = 1; i < numRow; i++) 
+0

感謝Merlevede。我把檢查像(list_theme.size()> = j),但它仍然是錯誤的。 – user3001046

+0

@ user3001046對不起,支票應該是>而不是> = – Merlevede

+0

,請在logcat的我的編輯問題中檢查它。我log.i檢查索引越界異常之前的最後我和j。 – user3001046