簡體   English   中英

android中表布局的索引超出范圍異常

[英]Index out of bounds Exception with table layout in 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

謝謝

我猜冒犯的行是:

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

您正在訪問數組中的項目j,但不檢查其范圍。 您只是假設其中有很多項目。 您需要添加類似

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM