修复或替换处理程序代码以自动随机

Fix or Replace the Handler code to auto-random

我在这里需要帮助,所以我想使用 Handler 每 2 秒自动随机化我的案例列表

完整代码是这样的

public class play extends Activity implements View.OnClickListener{

int number;
Button button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13, button14, button15, button16;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.play);


     button1 = (Button)findViewById(R.id.button1);
     button2 = (Button)findViewById(R.id.button2);
     button3 = (Button)findViewById(R.id.button3);
     button4 = (Button)findViewById(R.id.button4);
     button5 = (Button)findViewById(R.id.button5);
     button6 = (Button)findViewById(R.id.button6);
     button7 = (Button)findViewById(R.id.button7);
     button8 = (Button)findViewById(R.id.button8);
     button9 = (Button)findViewById(R.id.button9);
     button10 = (Button)findViewById(R.id.button10);
     button11 = (Button)findViewById(R.id.button11);
     button12 = (Button)findViewById(R.id.button12);
     button13 = (Button)findViewById(R.id.button13);
     button14 = (Button)findViewById(R.id.button14);
     button15 = (Button)findViewById(R.id.button15);
     button16 = (Button)findViewById(R.id.button16);

//i had use setOnClickListener(This) to make all of button do same logic in one method
// correct me if i wrong

       button1.setOnClickListener(this);
       button2.setOnClickListener(this);
       button3.setOnClickListener(this);
       button4.setOnClickListener(this);
       button5.setOnClickListener(this);
       button6.setOnClickListener(this);
       button7.setOnClickListener(this);
       button8.setOnClickListener(this);
       button9.setOnClickListener(this);
       button10.setOnClickListener(this);
       button11.setOnClickListener(this);
       button12.setOnClickListener(this);
       button13.setOnClickListener(this);
       button14.setOnClickListener(this);
       button15.setOnClickListener(this);
       button16.setOnClickListener(this);

}



// this is the method to make all of the button do same logic with one method
// correct me if i wrong

@Override
public void onClick(View v) {

//i use the handler method to do auto-random every 2 second
// correct me if i wrong

    final Handler mHandler = new Handler();
    final int ms=2000; //2secs.
//i got error in this line
// Define cannot be resolved to a type, Syntax error on token ":", { expected after this token, Syntax error, insert ";" to complete


    Define a Runnable: 

    final Runnable mUpdateTimeTask = new Runnable() {
        public void run() {
            Random generator = new Random();
            number = generator.nextInt(16) + 1; 


             // The '16' is the number of values

             // Here, we are checking to see what the output of the random was
             switch(number) { 

              case 1:  
              button1.setVisibility(View.VISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 2: 
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.VISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 3:  
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.VISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 4:  
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.VISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 5:  
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.VISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 6:  
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.VISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 7:  
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.VISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 8:  
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.VISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 9:  
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.VISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 10: 
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.VISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 11: 
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.VISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 12: 
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.VISIBLE);                   
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 13: 
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.VISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 14: 
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.VISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 15: 
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.VISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 16: 
              button1.setVisibility(View.VISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.VISIBLE);
              break;

           }


// the auto-random will be faster and faster
// correct me if i do it wrong too


            }
        };


        ms=ms-20; //20ms faster
        mHandler.postDelayed((Runnable) this, ms);
        mHandler.postDelayed(mUpdateTimeTask, 100); 

}  

}

在我搜索问题所在后,我发现确实是 Handler 方法导致的强制关闭:

final Handler mHandler = new Handler();
    int ms=2000; //2secs.



    final Runnable mUpdateTimeTask = new Runnable() {
    public void run() {

        //Do random logic

               }

        };

    ms=ms-20; //20ms faster
    mHandler.postDelayed((Runnable) this, ms);
    mHandler.postDelayed(mUpdateTimeTask, 100); 

 }
}

如果您看我的代码很疯狂,我很抱歉,因为我需要更多经验的人来解决当前问题,或者有人有一些示例代码来替换处理程序方法吗?

提前致谢。

Define a Runnable: 无效 Java。要么删除它,要么将其注释掉。

此外,要清理代码,请执行以下操作:

Button[] buttons = new Button[]{ button1, button2, ... button15,  button16 }; 
// fill in the other buttons above

Random generator = new Random();
number = generator.nextInt(16); 

for( int i=0; i<buttons.length; i++ ) {
   if( i == number )
       buttons[i].setVisibility( View.VISIBLE );
   else
       buttons[i].setVisibility( View.INVISIBLE );
}

最后,执行此操作时您可能会收到 ClassCastException:

mHandler.postDelayed((Runnable) this, ms);

this 在这种情况下可能是 Activity,而不是 Runnable。相反,只需执行:

mHandler.postDelayed( mUpdateTimeTask, ms );

并删除第二行。您还可以 post runnable 本身到 run() 方法中的处理程序:

final Handler mHandler = new Handler();
int ms=2000;

final Runnable mUpdateTimeTask = new Runnable() {
    public void run() {
        //Do random logic

        ms -= 20;
        if( ms > 0 ) // stop when delay is less than 0.
            mHandler.postDelayed( this, ms );
    }

};


// run this in a method.
mHandler.postDelayed(mUpdateTimeTask, ms); // post the first