获取数字格式异常?

时间:2013-04-04 08:19:54

标签: java android datetime null

我遇到了问题。我收到所有联系人和他们最近的calldates但是当我试图将calldate转换为date格式时,它会在long = null的值时抛出NumberFormatException。

我想知道如何将所有calldates转换为日期格式,但如果出现null,则不会将其转换为日期格式。下面是我的代码帮助plz ..

    mCursor.moveToFirst();

              while(!mCursor.isAfterLast()) {
                 String calldate="";

                    JSONObject contact = new JSONObject();
                    JSONObject parentCont = new JSONObject();
                    SharedPreferences settings = getSharedPreferences(Sesssion.PREF_NAME,0);
                    settings.getBoolean("phnosaved", true);  
                String usernumber=      settings.getString(Sesssion.knumber,"hey" );
                     String name = mCursor.getString(mCursor.getColumnIndex(Phone.DISPLAY_NAME));
                     String number = mCursor.getString(mCursor.getColumnIndex(Phone.NUMBER));
                     String selection = android.provider.CallLog.Calls.NUMBER+"="+"\""+number+"\"";
                     Log.d("selection query", "selection"+selection);
                     String Order = android.provider.CallLog.Calls.DATE + " DESC";
                              Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI,null, selection,null, null); 

                              if (managedCursor.moveToFirst ()) {
                                  do {

                            calldate= managedCursor.getString (managedCursor.getColumnIndex (android.provider.CallLog.Calls.DATE));
                                  break;

                                  } while (managedCursor.moveToNext());


                              }

                  //String   callDate = managedCursor.getString(managedCursor.getColumnIndex(android.provider.CallLog.Calls.DATE));

                    Date callDayTime = new Date(Long.valueOf(calldate));


                    //String dateColumn = cursorcalllog.getString(cursorcalllog.getColumnIndex(CallLog.Calls.DATE));

                     Log.d("CONTACT", "Name: " + name + "| Number: " + number+"Recently Called"+callDayTime);


logcat:-

04-03 13:13:52.231: E/AndroidRuntime(16548): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.socialize_us/com.example.socialize_us.PhoneBookActivity}: java.lang.NumberFormatException: Invalid long: ""
04-03 13:13:52.231: E/AndroidRuntime(16548):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2084)
04-03 13:13:52.231: E/AndroidRuntime(16548):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1924)
04-03 13:13:52.231: E/AndroidRuntime(16548):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
04-03 13:13:52.231: E/AndroidRuntime(16548):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)

2 个答案:

答案 0 :(得分:0)

你可以尝试

if (calldate != null && !calldate.isEmpty()) 
    Date callDayTime = new Date(Long.valueOf(calldate));

答案 1 :(得分:0)

问题是

Long.valueOf( “”)

将抛出异常

所以 在致电

之前
 Date callDayTime = new Date(Long.valueOf(calldate));

确保calldate或等于“”

喜欢

    if(!calldate.trim.equals("")) {    Date callDayTime = new Date(Long.valueOf(calldate));}