Question

I actually wanted a textView to change when set of pictures match with the background image of button array. I do not know why app crashes on opening because of the code below. Here is the code:

  protected void checkResult(int bx2[],int demon[]) {
    // TODO Auto-generated method stub
    final int[] cards1={R.drawable.one1,R.drawable.one2,
             R.drawable.one3,R.drawable.two1,R.drawable.two2,
             R.drawable.two3,R.drawable.three1,
             R.drawable.three2,0};

    int klop=0;
    for(int i=0; i<9;i++) {
       int sene = cards1[i];
       Button vl = (Button) findViewById(bx2[i]);
       if(vl.getBackground()==getResources().getDrawable(sene)) {    
               klop+=1;
               if(klop==7) {
                   TextView tv = (TextView) findViewById(R.id.textView1);
                   tv.setText("PUZZLE SOLVED!");
               }    
        }

    }           
}`
Was it helpful?

Solution

your code will crash with ResourceNotFoundException when you try to get the Drawable when i = 8 and sene = cards1[8] = 0; to avoid the crash you can add a simple check

int sene = cards1[i];
if (sene == 0)
 continue;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top