java - android unparseble date Exception? -


i making application in using date functionality,but looking after several existing posts, still not able simpledateformat parser working.

here code:

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 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 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 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 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  }   } 

and getting error

java.text.parseexception: unparseable date:"" 

you have this

 sdf1.parse(pref.getstring(start_time, "")); 

if default value returned empty string. natural java.text.parseexception: unparseable date:"". parsing empty string

public abstract string getstring (string key, string defvalue)  added in api level 1 retrieve string value preferences.  returns preference value if exists, or defvalue 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -