使用layoutInflater:onclicklistener将活动从第一个布局切换到第二个布局

时间:2014-07-19 18:15:03

标签: android android-layout onclicklistener

在这个应用程序中,我从包含drawables(名为vmarray [12])的数组中随机显示6个图像,1分钟后(使用runnable postdelayed)切换到包含id为BUTTON1的单个按钮并设置按钮的另一个布局背景到先前显示的图像中的一个图像。我正在使用LAYOUTINFLATER在布局之间切换

在那个按钮(Button1)onclick上我想做一些工作,但问题是当我实现onclicklistener和onclick方法时,应用程序崩溃了。

如果删除工具onclicklistener,一切运行正常。 BUTTON1完全通过findviewbyid定位,其背景正确设置为图像。但是实现onclicklister会破坏应用程序。

    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<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() < 6) {
     random1 = randomGenerator.nextInt(12);
     random2 = rand6.nextInt(6);

      if (!numbers.contains(random1) && (!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

1 个答案:

答案 0 :(得分:0)

我得到了它。在切换案例之后我可以运行以下按钮并且按钮onclick正在工作。在没有APP崩溃的情况下运行精细

                b11.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                b11.setBackgroundResource(vmarray[0]); //SOME RANDOM IMAGE FOR CHECKING
                }
             });