سؤال

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