动态分配设置onClick侦听器imageview

时间:2015-03-10 11:59:26

标签: android

我正在开发一个使用数百个图像视图的应用程序,我想动态设置onClick监听器和分配。 但不幸的是我得到了空指针例外 代码如下所示

ImageView simageView1, simageView2, simageView3, simageView4, simageView5;  
ImageView[] imgViewArray = { simageView1, simageView2, simageView3,
        simageView4, simageView5 };  
for (int i = 0; i < imgViewArray.length; i++) {
        int integere = i+1;
        String imageViewName = "simageView" + integere;
        Log.d("tag", "name of the ImageView are " +imageViewName);
        imgViewArray[i] = (ImageView) findViewById(getResources()
                .getIdentifier(imageViewName, "id", getPackageName()));
         imgViewArray[i].setOnClickListener(this);  

我收到NullPointerException

1 个答案:

答案 0 :(得分:0)

数以百计的ImageViews? 听起来你应该使用ListView / GridView / RecyclerView与适配器和onItemClicked监听器。 我认为这会好得多。

请参阅: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

http://developer.android.com/guide/topics/ui/layout/gridview.html