2014-04-14 45 views
1

我正在製作一個應用程序,我在使用日期功能,但照顧幾個現有的帖子,我仍然無法讓我的SimpleDateFormat解析器工作。android unparseble date異常?

下面是代碼:

public class GPSDemo extends Activity { 

public static String TAG = "Abhi's Log"; 

public static String IMEII = "imei"; 
public static String START_TIME = "startTime"; 
public static String WORKING_STATUS = "workingStatus"; 
public static String ADDRESS = "address"; 
public static int level = 0; 

Button stop, hide; 

SharedPreferences pref; 
Editor edit; 

Date d1 = null; 
Date d2 = null; 

String startingTime; 
String currentTime; 

Context ctx = this; 

TextView txt_start_time, txt_total_time, txt_total_distance, txt_address; 

@SuppressLint("NewApi") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.demoooo); 

    pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
    edit = pref.edit(); 

    ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(getBaseContext().CONNECTIVITY_SERVICE); 
    this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 

    txt_start_time = (TextView) findViewById(R.id.txt_start_time); 
    txt_total_time = (TextView) findViewById(R.id.txt_total_time); 
    txt_total_distance = (TextView) findViewById(R.id.txt_total_distance); 
    txt_address = (TextView) findViewById(R.id.txt_address); 

    try { 
     TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 


     Editor edit = PreferenceManager 
       .getDefaultSharedPreferences(
         getApplicationContext()).edit(); 
     edit.putString(IMEII, mTelephonyMgr.getDeviceId()); 
     edit.commit(); 
     Log.i(TAG, "IMEI Stored Sucessfully : " 
       + PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString(IMEII, "")); 
    } catch (Exception e) { 

     Log.d(TAG, "Exception While Storin Data : " + e); 
    } 

    //  turnGPSOn(this); 
    // turnGPSOn(); 


    if(!isInternetOn()) { 
     try { 
     Method method = connectivityManager.getClass().getMethod("setMobileDataEnabled", boolean.class); 
     method.invoke(connectivityManager, true); 
     } catch (NoSuchMethodException e) { 
     e.printStackTrace(); 
     } catch (Exception e) { 
     e.printStackTrace(); 
     } 
    } 


    stop = (Button) findViewById(R.id.btn_stop); 
    hide = (Button) findViewById(R.id.btn_hide); 

    try { 

     SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); 
     if(pref.getString(WORKING_STATUS, "").equals("NO")) { 
      Intent intent = new Intent(GPSDemo.this, NetworkTrackerService.class); 

      startService(intent); 
      edit.putString(WORKING_STATUS, "YES"); 
      edit.putString(START_TIME, sdf.format(new Date())); 
      edit.commit(); 
      txt_start_time.setText(sdf.format(new Date())); 
     } else if(pref.getString(WORKING_STATUS, "").equals("YES")) { 
      txt_start_time.setText(pref.getString(START_TIME, "")); 
     } 
     updateInof(); 
     startInfoUpdater(); 
     Log.i("Abhi's Log", "Values Stored :: " +pref.getString(WORKING_STATUS, "") +" " + pref.getString(START_TIME, "")); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


    stop.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent intent = new Intent(GPSDemo.this, NetworkTrackerService.class); 
      edit.putString(WORKING_STATUS, "NO"); 
      edit.putString(START_TIME, ""); 
      edit.putString(ADDRESS, ""); 
      edit.commit(); 
      stopService(intent); 

      System.exit(0); 
     } 
    }); 

    hide.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      GPSDemo.this.finish(); 
     } 
    }); 



} 

@SuppressLint("NewApi") 
@SuppressWarnings("deprecation") 
public void turnGPSOn(Context context) 
{ 
    Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); 
    intent.putExtra("enabled", true); 
    context.sendBroadcast(intent); 

    String provider = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
    if (! provider.contains("gps")) 
    { //if gps is disabled 
     final Intent poke = new Intent(); 
     poke.setClassName("com.tracking.gps", "com.android.settings.widget.SettingsAppWidgetProvider"); 
     poke.addCategory(Intent.CATEGORY_ALTERNATIVE); 
     poke.setData(Uri.parse("3")); 
     context.sendBroadcast(poke); 
    } 
} 

public void turnGPSOn() 
{ 
    Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); 
    intent.putExtra("enabled", true); 
    this.ctx.sendBroadcast(intent); 

    String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
    if(!provider.contains("gps")) 
     { 
     //if gps is disabled 
     final Intent poke = new Intent(); 
     poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
     poke.addCategory(Intent.CATEGORY_ALTERNATIVE); 
     poke.setData(Uri.parse("3")); 
     this.ctx.sendBroadcast(poke); 
    } 
} 

@SuppressLint("NewApi") 
@SuppressWarnings("deprecation") 
public static void turnGPSOff(Context context) 
{ 
    String provider = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
    if (provider.contains("gps")) 
    { //if gps is enabled 
     final Intent poke = new Intent(); 
     poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
     poke.addCategory(Intent.CATEGORY_ALTERNATIVE); 
     poke.setData(Uri.parse("3")); 
     context.sendBroadcast(poke); 
    } 
} 
private void startInfoUpdater() { 
    // TODO Auto-generated method stub 
    final Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
     //Do something after 100ms 

      Log.v("Abhi's Tag", "Inside Schedulat ... !!!"); 

     updateInof(); 


      startInfoUpdater(); 
     } 
    }, 60000); 
} 
protected void updateInof() { 
    // TODO Auto-generated method stub 
    long diffSeconds, diffMinutes = 0, diffHours = 0, diffDays = 0; 

     SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); 
     currentTime = sdf1.format(new Date()); 

     try { 
      d1 = sdf1.parse(pref.getString(START_TIME, "")); 
      d2 = sdf1.parse(currentTime); 

      long diff = d2.getTime() - d1.getTime(); 

      diffSeconds = diff/(1000); 
      diffMinutes = diff/(60 * 1000); 
      diffHours = diff/(60 * 60 * 1000); 
      diffDays = diff/(24 * 60 * 60 * 1000); 

      Log.i("Abhi's Tag", "Days : " + diffDays); 
      Log.i("Abhi's Tag", "Hours : " + diffHours); 
      Log.i("Abhi's Tag", "Minutes : " + diffMinutes); 
      Log.i("Abhi's Tag", "Seconds : " + diffSeconds); 

    } catch (ParseException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


     txt_start_time.setText(pref.getString(START_TIME, "")); 
     txt_total_time.setText(diffDays+" D, " + diffHours + " H, " + diffMinutes + " M"); 
     txt_address.setText(pref.getString(ADDRESS, "")); 
} 
private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0); 

     SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context); 
     Editor edit = pref.edit(); 
     edit.putInt("batteryStatus", level); 
     edit.commit(); 


    } 
}; 

public final boolean isInternetOn() { 

    ConnectivityManager connec = (ConnectivityManager)getSystemService(getBaseContext().CONNECTIVITY_SERVICE); 

     if (connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED || 
      connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING || 
      connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING || 
      connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED) { 

      return true; 

     } else if (connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED || 
       connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED ) { 

      return false; 
     } 
     return false; 
    } 
@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 

} 


} 

,我得到這個錯誤

java.text.ParseException: Unparseable date:"" 

回答

0

你有這樣的

sdf1.parse(pref.getString(START_TIME, "")); 

如果默認值返回它的一個空字符串。所以它的自然你得到java.text.ParseException: Unparseable date:""。您解析空字符串

public abstract String getString (String key, String defValue) 

Added in API level 1 
Retrieve a String value from the preferences. 

Returns the preference value if it exists, or defValue 
+0

好的先生,我嘗試三江源... :) – User11

+0

先生,你能elobrate更,,請......,我沒有得到proprly – User11

+0

@ R.T。如果存在'START_TIME'的首選項值,則獲得該字符串。如果沒有,默認情況下會得到''「''。它是一個空字符串,你解析爲'sdf1.parse(「」);'爲什麼你會得到異常 – Raghunandan