문제

Here's the code snippet

 s= e1.getText().toString();
            iv1 = (ImageView) findViewById(R.id.imageView1);
            iv1.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(s.matches("")){
                        int time=2000;
                        Toast.makeText(Loadscreen.this, "Please specify the level", time).show();
                    }
                    else{
                        int str= Integer.valueOf(s);
                        if(str>0 && str<11){
                            calculate(str);
                            r2=new Random();
                            int nxt = r2.nextInt(2);
                            if(nxt==0){
                                                       //do some work
                            }
                            else{
                                //do some work
                            }
                        }
                        else{
                            int time=2000;
                            Toast.makeText(Loadscreen.this, "Enter a valid level", time).show();
                        }
                    }
                }
            });

Control never goes to the else block. The output on the screen is the text of the Toast with the comment "Please specify the level". Please suggest why else part is not executed?

도움이 되었습니까?

해결책

probably you are not getting the correct result while getting the text from edit text

write this line in onclick method before any comparison

 s= e1.getText().toString();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top