NumberFormatException

2019-03-05 03:34发布

问题:

I'm trying to develop an app that will take a number from the dial pad and call another number instead. But i get a number format exception on the phonenumber i want to call.

this is the code.

    public class testing extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {
     if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {


              // get phone number from bundle
                String phoneNumber = intent.getExtras().getString(Intent.EXTRA_PHONE_NUMBER);

               // Toast.makeText(context, "Call:  "+phoneNumber+" - ABORTING CALL.", Toast.LENGTH_LONG).show();
              //  
                if (phoneNumber == "") {
                    phoneNumber = "0";
                }
                    int checknumber  = Integer.parseInt(phoneNumber);

                if (checknumber == 4444) {
                    Toast.makeText(context, "hello", Toast.LENGTH_LONG).show();
                   setResultData(null);

                  Intent callIntent = new Intent(Intent.ACTION_CALL);
               callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                   callIntent.setData(Uri.parse("tel:6768886877"));
                   context.startActivity(callIntent);




                }
                else {
                    Toast.makeText(context, "kkkk", Toast.LENGTH_LONG).show();

                }
          }


        }

       }

and below is my the log of the error

    01-04 20:16:24.941: E/AndroidRuntime(28536): java.lang.RuntimeException: Unable to start receiver vu.alhadji.vudial.testing: java.lang.NumberFormatException: Invalid int: "2156689451"
    01-04 20:16:24.941: E/AndroidRuntime(28536):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2126)
    01-04 20:16:24.941: E/AndroidRuntime(28536):    at android.app.ActivityThread.access$1500(ActivityThread.java:123)
    01-04 20:16:24.941: E/AndroidRuntime(28536):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197)
01-04 20:16:24.941: E/AndroidRuntime(28536):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 20:16:24.941: E/AndroidRuntime(28536):    at android.os.Looper.loop(Looper.java:137)
01-04 20:16:24.941: E/AndroidRuntime(28536):    at android.app.ActivityThread.main(ActivityThread.java:4424)
01-04 20:16:24.941: E/AndroidRuntime(28536):    at java.lang.reflect.Method.invokeNative(Native Method)
01-04 20:16:24.941: E/AndroidRuntime(28536):    at java.lang.reflect.Method.invoke(Method.java:511)
01-04 20:16:24.941: E/AndroidRuntime(28536):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:812)
01-04 20:16:24.941: E/AndroidRuntime(28536):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:579)
01-04 20:16:24.941: E/AndroidRuntime(28536):    at dalvik.system.NativeStart.main(Native Method)
01-04 20:16:24.941: E/AndroidRuntime(28536): Caused by: java.lang.NumberFormatException: Invalid int: "2156689451"
01-04 20:16:24.941: E/AndroidRuntime(28536):    at java.lang.Integer.invalidInt(Integer.java:138)
01-04 20:16:24.941: E/AndroidRuntime(28536):    at java.lang.Integer.parse(Integer.java:378)
01-04 20:16:24.941: E/AndroidRuntime(28536):    at java.lang.Integer.parseInt(Integer.java:366)
01-04 20:16:24.941: E/AndroidRuntime(28536):    at java.lang.Integer.parseInt(Integer.java:332)
01-04 20:16:24.941: E/AndroidRuntime(28536):    at vu.alhadji.vudial.testing.onReceive(testing.java:24)
01-04 20:16:24.941: E/AndroidRuntime(28536):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2119)
01-04 20:16:24.941: E/AndroidRuntime(28536):    ... 10 more

回答1:

That's because 6768886877 is too big of a number for an int.

Use a long instead.

long checkNumber = 0l;

try{
  checkNumber = Long.parseLong (phoneNumber);
}
catch (NumberFormatException e)
{
  e.printStackTrace();
}


回答2:

2156689451 is too big for an int (which range from -2^31≈-2,147E9 to +2^31-1≈2,147E9, whereas your number is ~2,15E10)

Use long instead of int...