0

I want to make an calculate the outside its two-digit division that the user use edit box to input the two numbers and when the user leave one edit text empty he git an error messages without crushing the app i tried to it but the app always crush

public class MainActivity extends AppCompatActivity {
    private EditText a1,a2;
    private Button s;
    private TextView d;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        a1=findViewById( R.id.editTextTextPersonName );
        a2=findViewById( R.id.editTextTextPersonName2 );
        s=findViewById( R.id.button );
        d=findViewById( R.id.textView );
        s.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                float aa = Float.parseFloat( a1.getText().toString() );
                float ss = Float.parseFloat( a2.getText().toString() );
                float f = aa / ss;
                if(a1.getText().toString()==null){
                    a1.setError( "n1 is required" );
                    a1.requestFocus();
                }else if(a2.getText().toString()==null){
                    a2.setError( "n1 is required" );
                    a2.requestFocus();
                }else {
                    d.setText( (int) f );
                }
            }
        });
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • 2
    This does not appear to be JavaScript – CertainPerformance Mar 15 '21 at 14:48
  • Welcome to SO. Please do yourself and us a favor and work on your punctuation (and ideally your spelling - e.g. crash vs. crush) to make your questions easier to understand. The harder it is to understand our question the less likely it will be that people are willing to help. And while you're at it please read [ask] :) – Thomas Mar 15 '21 at 15:07
  • Most likely, you either have a null pointer exception because your "wiring" is wrong, and some fields are null, or you try for example to parse an empty string, which results in a NumberFormatException. The real point is: this stuff is complicated. You have to do plenty of research and learning. – GhostCat Mar 15 '21 at 15:14
  • In other words: you dont start by sending some code to other people on stackoverflow. Instead, you have to learn how to get to the **relevant** information when running your code. Then you should carefully research that information and try to solve it yourself. And only then, when you tried for quite some while, and you are really stuck, then you should create a question with a good [mcve] here. – GhostCat Mar 15 '21 at 15:15
  • And then: use meaningful names that tell the HUMAN reader what your variables are about. a1, a2, s, d ... tell the reader NOTHING. – GhostCat Mar 15 '21 at 15:16

2 Answers2

0

You just have to check if edit text is empty or not.

Try below Code

String FirstEdit = a1.getText().toString();
String SecondEdit = a2.getText().toString();

if (TextUtils.isEmpty(FirstEdit) || TextUtils.isEmpty(SecondEdit))
{
//Your Message
}
Sahil Goyal
  • 415
  • 3
  • 13
  • You are guessing. We have no clue why the app is crashing. – GhostCat Mar 15 '21 at 15:18
  • FirstEdit and SecondEdit are numbers that used to calculate the division so thay cant be String if i did it but in integer i wont be able to use TextUtils – Mohamed Fady Fouad Mar 15 '21 at 15:40
  • @GhostCat the app is crushing couse off NumberFormatException and i don't know how to fix it – Mohamed Fady Fouad Mar 15 '21 at 15:43
  • @MohamedFadyFouad I already told you what to do then: A) research that problem (there are many many questions about this on this site, and zillions of tutorials and resources on the internet that EXPLAIN what the exception means, and how to do about it) ... and only if you really cant solve it yourself then B) ask a question specifically about that, with a real [mcve]. But as said: that is most likely not necessary. READ the exception message. It TELLS you what string it is trying to parse. Most likely your string is empty, or contains spaces. – GhostCat Mar 15 '21 at 15:48
  • You should thus check whether the string is empty before trying to parse it. If it is not empty, try calling `parse(... theString.trim())` instead, to get rid of whitespaces. – GhostCat Mar 15 '21 at 15:48
0

If the app is crashing ,check the log window for the type of error or exception.If log window shows some nullPointerException() then typecast the members a1,a2,s,d with their respective type.ie... (EditText),(TextView),(Button).And if this exception is not showing than also make a habit to typecast members always to avoid any hassle.

If after that also there comes some issue than,make these changes over the code ---------------------------------->
if(TextUtils.isEmpty(a1.getText().toString()))
a1.setError("n1 is required" );
else if(TextUtils.isEmpty(a1.getText().toString())){
                    a2.setError( "n1 is required" );
                    a2.requestFocus();
                }else {
                    d.setText( (int) f );
                }