[Solved] Android force close when using setBackgroundColor and setImageDrawable in loop [closed]


You need to create imageview dynamically.

ImageView images[];
     View shelfRow[] =new View[numberOfRows];
    for (int i = 0; i < numberOfRows; i++) {
        images = new ImageView[numberOfRows];

        shelfRow[i].setBackgroundResource(R.drawable.shelf_row2);
        images[i].setBackgroundColor(android.R.color.black);
        parentPanel.addView(shelfRow[i]);
    }

Or create 10 imageviews and give id to it like..

int[] buttonIDs = new int[] {R.id.button1ID, R.id.button2ID, R.id.button3ID, ... };
              View shelfRow[] =new View[numberOfRows];
    ImageView[] forAdapter = new ImageView[numberOfRows];
    for (int i = 0; i < numberOfRows; i++) {
        forAdapter[i] = (ImageView) findViewById(buttonIDs[i]);

        shelfRow[i].setBackgroundResource(R.drawable.shelf_row2);
        forAdapter[i].setBackgroundColor(android.R.color.black);
        parentPanel.addView(shelfRow[i]);
    }

solved Android force close when using setBackgroundColor and setImageDrawable in loop [closed]