0

I am trying to make a simple android app in which there is a toast message containing the number of the incoming call. I have stored the number in a string variable. When I display just the toast, it works fine but before the toast, if I add a simple if condition comparing the number to another string, the app quits. The required permissions are given. Can someone help me?

This Works:

public void onReceive(Context context, Intent intent) {

        String incomingNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Toast.makeText(context, incomingNumber, Toast.LENGTH_LONG).show();
    }

This Does not Work (App quits)

public void onReceive(Context context, Intent intent) {

        String incomingNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
          if(incomingNumber.equals("+919999999999"))
           {
              Toast.makeText(context, "Call from Mom", Toast.LENGTH_LONG).show();
           }
           else
           {
              Toast.makeText(context, incomingNumber, Toast.LENGTH_LONG).show();
           }

    }
Aaditya Joshi
  • 122
  • 10
  • "the app quits". Do you get an error? have you debugged your code? what is that value, a default? I don't see anything in that condition or that code that would just "quit" – Stultuske Apr 23 '20 at 05:18
  • Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – a_local_nobody Apr 23 '20 at 05:37
  • Maybe if you change .equals() to == it will work – Lenin Apr 23 '20 at 05:51
  • @Lenin not if it's java code. for javascript, that would be fine, but it isn't javascript – Stultuske Apr 23 '20 at 05:56
  • it won't @Lenin that's just going to cause more bugs. – a_local_nobody Apr 23 '20 at 05:57

1 Answers1

0

It sounds a little silly but I just wrapped my "if" condition in a try-catch block and it solved the problem. I am not used to programming in JAVA so it took me a lot to figure out this simple thing. Thank You All for providing me with suggestions :)

Aaditya Joshi
  • 122
  • 10