0

我面临一个问题。我正在接收所有联系人及其最近的通话日期,但是当我尝试将通话日期转换为日期格式时,当 long =null 的值时它会抛出 NumberFormatException。

我想知道如何将所有调用日期转换为日期格式,但如果出现 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)
4

2 回答 2

0

你可以试试

if (calldate != null && !calldate.isEmpty()) 
    Date callDayTime = new Date(Long.valueOf(calldate));
于 2013-04-04T08:22:57.030 回答
0

问题是

Long.valueOf("")

会抛出异常

所以在调用之前

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

确保 calldate 也不等于 ""

    if(!calldate.trim.equals("")) {    Date callDayTime = new Date(Long.valueOf(calldate));}
于 2013-04-04T08:41:23.987 回答