Title: Onclick linear layout , return view id always -1
Tags: android;view
Question: In my app , I try to generate dynamic arrays of view  and try to add onclick listener but onclick that view it is returning   java.lang.ArrayIndexOutOfBoundsException: length=5; index=-1 and view id return always -1, please help me how to solve it.
Below is my code:-

```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);
            }

        }
    };
```




{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:05/31/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><view>', 'Title': 'Onclick linear layout , return view id always -1', 'ViewCount': 304, 'id': 10831078, 'user_AboutMe': '<p>Android Developer, Variable3 Technologies, Bangalore</p>\n', 'user_AccountId': 382361.0, 'user_DisplayName': 'Narendra', 'user_Reputation': 1858.0, 'user_Views': 281.0, 'user_id': 737540.0}}