开发者

Toast didnt work

I have android project v.2.1. and I have a code, in the code I have Toast, but it doesn't work. Why?

    public void click(){

    TextView color = (TextView) findViewById(R.id.text);
    switch(i){

    case 1: table.setBackgroundColor(Color.RED); color.setText("Красный");
        break;
    case 2: table.setBackgroundColor(Color.rgb (255, 127, 0) );
    color.setText("Оранжевый");
        break;
    case 3: table.setBackgroundColor(Color.YELLOW);
    color.setText("Желтый");
        break;
    case 4: table.setBackgroundColor(Color.GREEN) ;
    color.setText("Зеленый");
        break;
    case 5: table.setBackgroundColor(Color.rgb (0,191,255) );
    color.setText("Голубой");
        break;
    case 6: table.setBackgroundColor(Color.BLUE );
    color.setText("Синий");
        break;
    case 7: table.setBackgroundColor(Color.rgb (160,32,240) );
    color.setText("Фиолетовый");
        break;
    case 8: //  i=1; 
    Toast.makeText(getApplicationContext(), "Вы прошли вс开发者_如何学Goе цвета",Toast.LENGTH_SHORT).show(); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< MY TOAST!
        break;
    }

Toast not in an OnCreate.


Your statement to create and show the Toast is correct. It is likely you are having a logic error are you absolutely positive that your switch statement is using case 8? It would be a good idea to put some log statements inside each case that print out which case it is so you can debug what is happening easier.

Im not certain what the rest of your activity looks like but this appears to be some kind of click listener callback method. If that is the case you should not have

TextView color = (TextView) findViewById(R.id.text);

Inside a click callback. Doing so means that findViewById() is going to get called every time your view gets clicked. Since this is a relatively expensive method to call its more appropriate to call it once in your onCreate and just keep the reference that you get back for all other times you'll need it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜