NumberFormatException的(NumberFormatException)

2019-08-05 16:26发布

我想开发一个应用程序,会从拨号盘号码和拨打另一个号码代替。 但我得到我要拨打的PHONENUMBER的数字格式例外。

这是代码。

    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();

                }
          }


        }

       }

以下是我的错误日志

    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

Answer 1:

这是因为6768886877是数字太大了,为的int

使用long代替。

long checkNumber = 0l;

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


Answer 2:

2156689451为一个int过大(其范围从-2 ^31≈-2,147E9至+ 2 ^31-1≈2,147E9,而你的电话号码是〜2,15E10)

使用long而不是int ...



文章来源: NumberFormatException