title: Switching from first layout to second using layoutInflater: onclicklistner crashing the activity
body: <p>In this application i am displaying 6 images randomly from an array containing drawables(named vmarray[12] ) and after 1 minute(using runnable postdelayed) switching is performed to another layout containing a single button with id BUTTON1 and setting the button background to one of the image from the earlier displayed images. I am using LAYOUTINFLATER for switching between layouts</p>
  
  <p>On that button (Button1) onclick i want to do some work, but the PROBLEM IS when i implement onclicklistener and onclick method, the app crashes.</p>
  
  <p>IF i remove the implements onclicklistener everything runs fine. The BUTTON1 is located perfectely through findviewbyid and its background correctly sets to an image. BUT Implementing onclicklister is crashing the app.</p>
  
  <pre><code>    here is the code..
  
     int[] vmarray= {R.drawable.vm1bulb, R.drawable.vm2chair, R.drawable.vm3comb,
     R.drawable.vm4cycle,R.drawable.vm5dairy,R.drawable.vm6fan,R.drawable.vm7mobile,
     R.drawable.vm8pen,R.drawable.vm9shoes,R.drawable.vm10toothbrush,  
      R.drawable.vm11bangle, R.drawable.vm12watch};
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
       // TODO Auto-generated method stub
  
   super.onCreate(savedInstanceState);
  
  
   inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
  
   setContentView(R.layout.cacrvisualmem);
  
   firstview =(LinearLayout)this.findViewById(R.id.firsthumlayout);
  
   secondview = (LinearLayout) inflater.inflate(R.layout.cacrvisualmempart1, null);
  
     for(int i=0;i&lt;6;i++)
       { 
       final int a = i;
       button_var[a] = (Button)findViewById(idArray[a]); 
       }
  
  
      // IN FIRST LAYOUT, THERE ARE 6 BUTTONS AND THEIR BACKGROUND IS RANDOMLY SET      
     //   DRAWABLES FROM ARRAY "vmarray". the following code is for that
  
       Random randomGenerator = new Random();
        Random rand6 = new Random();
  
       while (numbers.size() &lt; 6) {
       random1 = randomGenerator.nextInt(12);
       random2 = rand6.nextInt(6);
  
        if (!numbers.contains(random1) &amp;&amp; (!numbers2.contains(random2))) 
         {
             numbers.add(random1);
  
             numbers2.add(random2);  
  
             b[random2].setBackgroundResource(vmarray[random1]);
             count++;         
  
          }//if ends
       }//while ends
  
       Handler changeview = new Handler();
  
       Runnable r1 = new Runnable(){
          @Override
          public void run() {
              // TODO Auto-generated method stub
  
              // NOW, here, after 1 minute, second view will be inflated and button1  
              //  background is set to one the image from the array
  
              setContentView(secondview);
  
              b11=(Button)findViewById(R.id.button1);
  
              queryrandvar=queryrand.nextInt(12);
  
              switch(queryrandvar)
              {
              case 0:
                  b11.setBackgroundResource(vmarray[0]);
                  queryval=0;
                  break;
              case 1:
                  b11.setBackgroundResource(vmarray[1]);
                  queryval=1;
                  break;
              case 2:
                  b11.setBackgroundResource(vmarray[2]);
                  queryval=2;
                  break;
              case 3:
                  b11.setBackgroundResource(vmarray[3]);
                  queryval=3;
                  break;
              case 4:
                  b11.setBackgroundResource(vmarray[4]);
                  queryval=4;
                  break;
              case 5:
                  b11.setBackgroundResource(vmarray[5]);
                  queryval= 5;
                  break;
              case 6:
                  b11.setBackgroundResource(vmarray[6]);
                  queryval= 6;
                  break;
              case 7:
                  b11.setBackgroundResource(vmarray[7]);
                  queryval= 7;
                  break;
              case 8:
                  b11.setBackgroundResource(vmarray[8]);
                  queryval= 8;
                  break;
              case 9:
                  b11.setBackgroundResource(vmarray[9]);
                  queryval= 9;
                  break;
              case 10:
                  b11.setBackgroundResource(vmarray[10]);
                  queryval= 10;
                  break;
              case 11:
                  b11.setBackgroundResource(vmarray[11]);
                  queryval= 11;
                  break;
  
          }//switch ends
  
  
          }//run ends
       };//runnable ends
  
       b11.setOnClickListener(null);
  
       changeview.postDelayed(r1,10000);
  
   } //on create method ends here
  
  
  
  @Override
  public void onClick(View v) {
      // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.button1:
       {
  
        b11.setBackgroundResource(vmarray[1]);
  
        break;
       }
  
    }//switch ends
  }//onclick ends
  </code></pre>