아래와 같은 소스가 있습니다.

1  public LinearLayout layout(int img_num) {
2    
3    cnt++; //생성 갯수

4    layout_a = new LinearLayout(this);
5    
6    text_t = new TextView(this);
7    tree_img[cnt] = new ImageView(this);

8  
9    
10   if (img_num == 1) {
11   tree_img[cnt].setBackgroundResource(R.drawable.tree_1);
12    } else if (img_num == 2) {
13    tree_img[cnt].setBackgroundResource(R.drawable.tree_2);
14    } else if (img_num == 3) {
15     tree_img[cnt].setBackgroundResource(R.drawable.tree_3);
16   } else if (img_num == 4) {
17     tree_img[cnt].setBackgroundResource(R.drawable.tree_4);
18    }
19     tree_img[cnt].setLayoutParams(new Gallery.LayoutParams(
20          LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
21    layout_a.addView(tree_img[cnt]);

22    return layout_a;
23   }

 

이렇게 하니 7번 줄에서 어플이 종료 됩니다.

ImageView를 배열로 생성하여 OnClickLisner를 사용하려 하는데

해결 방안이나 다른 방법있으면 조언 얻고자 합니다.