私のアプリでは、ビューの動的配列を生成して onclick リスナーを追加しようとしましたが、そのビューをオンクリックすると java.lang.ArrayIndexOutOfBoundsException: length=5;が返されます。index=-1とビュー ID は常に -1 を返します。解決方法を教えてください。以下は私のコードです: -
private void setLayout() {
layoutHeader = new LinearLayout[5];
layoutTable = new LinearLayout[5];
for (int i = 0; i <5; i++) {
layoutHeader[i] = new LinearLayout(this);
layoutHeader[i].setLayoutParams( new LayoutParams(LayoutParams.FILL_PARENT, 50));
layoutHeader[i].setBackgroundColor(R.color.background);
LayoutParams vName = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(this);
tv.setLayoutParams(vName);
tv.setText("Reliance");
layoutHeader[i].addView(tv);
LayoutParams pts = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tvPts=new TextView(this);
tvPts.setLayoutParams(pts);
tvPts.setText("12345");
tvPts.setGravity(0);
layoutHeader[i].addView(tvPts);
myWalletLayout.setId(i);
myWalletLayout.addView(layoutHeader[i]);
layoutHeader[i].setOnClickListener(onClickHeader);
//////////////////////////////////////////////////////////////////////////////////////
//////////SET CHILD VIEW/////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
layoutTable[i] = new LinearLayout(this);
layoutTable[i].setLayoutParams( new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
layoutTable[i].setId(12345);
layoutTable[i].setVisibility(View.GONE);
TableLayout MainLayout = new TableLayout(this);
MainLayout.setVerticalFadingEdgeEnabled(true);
MainLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
MainLayout.setStretchAllColumns(true);
MainLayout.setFadingEdgeLength(4);
//Create the first row and add two text views
TableRow row = new TableRow(this);
row.setPadding(1, 1, 1, 1);
row.setBackgroundColor(Color.parseColor("#ABA8A8"));
TextView txt1 = new TextView(this);
txt1.setText("Card Number");
txt1.setTextColor(Color.parseColor("#000000"));
txt1.setGravity(android.view.Gravity.CENTER);
TextView txt2 = new TextView(this);
txt2.setText("1234545");
txt2.setTextColor(Color.parseColor("#000000"));
txt2.setGravity(android.view.Gravity.CENTER);
row.addView(txt1);
row.addView(txt2);
MainLayout.addView(row);
TableRow row1 = new TableRow(this);
row1.setPadding(1, 1, 1, 1);
row1.setBackgroundColor(Color.parseColor("#ABA8B8"));
TextView txt3 = new TextView(this);
txt3.setText(" Barcode");
txt3.setTextColor(Color.parseColor("#000000"));
txt3.setGravity(android.view.Gravity.CENTER);
TextView txt4 = new TextView(this);
txt4.setText("1234545");
txt4.setTextColor(Color.parseColor("#000000"));
txt4.setGravity(android.view.Gravity.CENTER);
row1.addView(txt3);
row1.addView(txt4);
MainLayout.addView(row1);
layoutTable[i].addView(MainLayout);
layoutTable[i].setId(i);
myWalletLayout.addView(layoutTable[i]);
}
}
OnClickListener onClickHeader = new OnClickListener() {
public void onClick(View v) {
int currSelId = v.getId();
System.out.println("=============================="+currSelId);
if(layoutTable[currSelId].getVisibility()==8){
layoutTable[currSelId].setVisibility(View.VISIBLE);
}
else{
layoutTable[currSelId].setVisibility(View.GONE);
}
}
};