NumberFormatException的

时间:2013-01-05 01:31:10

标签: android broadcastreceiver numberformatexception

我正在尝试开发一个应用程序,它将从拨号盘中取一个号码,然后拨打另一个号码。但是我想要拨打的电话号码上有一个数字格式例外。

这是代码。

    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

2 个答案:

答案 0 :(得分:0)

那是因为6768886877对于int来说太大了。

改为使用long

long checkNumber = 0l;

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

答案 1 :(得分:0)

2156689451对于int来说太大了(范围从-2 ^31≈-2,147E9到+ 2 ^31-1≈2,147E9,而你的数字是~2,15E10)

使用long代替int ...