Error Saving a List in a xml File in Android -


i have implemented code capable of detecting ble signals , show them in listview (in each item of list show name, address , rssi) when try save xml file occurs error , stops app. here code:

package com.example.newblescan;  import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.objectoutputstream; import java.io.serializable;  import com.example.newblescan.r; import android.app.activity; import android.app.listactivity; import android.bluetooth.bluetoothadapter; import android.bluetooth.bluetoothdevice; import android.bluetooth.bluetoothmanager; import android.content.context; import android.content.intent; import android.content.pm.packagemanager; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.listview; import android.widget.toast;  import com.example.newblescan.adapter.bledevicesadapter;   /**  * activity scanning , displaying available bluetooth le devices.  */ public class devicescanactivity extends listactivity {  private static final int request_enable_bt = 1; private static final long scan_period = 500;  private bledevicesadapter ledevicelistadapter; private bluetoothadapter bluetoothadapter; private scanner scanner; private save save;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     getactionbar().settitle(r.string.title_devices);      // use check determine whether ble supported on device.  can     // selectively disable ble-related features.     if (!getpackagemanager().hassystemfeature(packagemanager.feature_bluetooth_le)) {         toast.maketext(this, r.string.ble_not_supported, toast.length_short).show();         finish();         return;     }      // initializes bluetooth adapter.  api level 18 , above, reference     // bluetoothadapter through bluetoothmanager.     final bluetoothmanager bluetoothmanager =             (bluetoothmanager) getsystemservice(context.bluetooth_service);     bluetoothadapter = bluetoothmanager.getadapter();      // checks if bluetooth supported on device.     if (bluetoothadapter == null) {         toast.maketext(this, r.string.error_bluetooth_not_supported, toast.length_short).show();         finish();         return;     } }  @override public boolean oncreateoptionsmenu(menu menu) {     getmenuinflater().inflate(r.menu.gatt_scan, menu);     if (scanner == null || !scanner.isscanning()) {         menu.finditem(r.id.menu_stop).setvisible(false);         menu.finditem(r.id.menu_scan).setvisible(true);         menu.finditem(r.id.menu_refresh).setactionview(null);     } else {         menu.finditem(r.id.menu_stop).setvisible(true);         menu.finditem(r.id.menu_scan).setvisible(false);         menu.finditem(r.id.menu_refresh).setactionview(                 r.layout.actionbar_indeterminate_progress);     }     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     switch (item.getitemid()) {         case r.id.menu_scan:             ledevicelistadapter.clear();             if (scanner == null) {                 scanner = new scanner(bluetoothadapter, mlescancallback);                 scanner.startscanning();                  invalidateoptionsmenu();             }             break;         case r.id.menu_stop:             if (scanner != null) {                 save = new save(ledevicelistadapter);                 scanner.stopscanning();                 try {                     save.savedata();                 } catch (filenotfoundexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }                 scanner = null;                   invalidateoptionsmenu();             }             break;     }     return true; }  @override protected void onresume() {     super.onresume();      // ensures bluetooth enabled on device.  if bluetooth not enabled,     // fire intent display dialog asking user grant permission enable it.     if (!bluetoothadapter.isenabled()) {         final intent enablebtintent = new intent(bluetoothadapter.action_request_enable);         startactivityforresult(enablebtintent, request_enable_bt);         return;     }      init(); }  @override protected void onactivityresult(int requestcode, int resultcode, intent data) {     // user chose not enable bluetooth.     if (requestcode == request_enable_bt) {         if (resultcode == activity.result_canceled) {             finish();         } else {             init();         }     }     super.onactivityresult(requestcode, resultcode, data); }  @override protected void onpause() {     super.onpause();      if (scanner != null) {         scanner.stopscanning();         scanner = null;     } }  @override protected void onlistitemclick(listview l, view v, int position, long id) {     final bluetoothdevice device = ledevicelistadapter.getdevice(position);     if (device == null)         return;      //final intent intent = new intent(this, deviceservicesactivity.class);     //intent.putextra(deviceservicesactivity.extras_device_name, device.getname());     //intent.putextra(deviceservicesactivity.extras_device_address, device.getaddress());     //startactivity(intent); }    private void init() {     if (ledevicelistadapter == null) {         ledevicelistadapter = new bledevicesadapter(getbasecontext());         setlistadapter(ledevicelistadapter);     }      if (scanner == null) {         scanner = new scanner(bluetoothadapter, mlescancallback);         scanner.startscanning();     }      invalidateoptionsmenu(); }  // device scan callback. private bluetoothadapter.lescancallback mlescancallback =         new bluetoothadapter.lescancallback() {              @override             public void onlescan(final bluetoothdevice device, final int rssi, byte[] scanrecord) {                 runonuithread(new runnable() {                     @override                     public void run() {                         ledevicelistadapter.adddevice(device, rssi);                         ledevicelistadapter.notifydatasetchanged();                     }                 });             }         };    private static class scanner extends thread {     private final bluetoothadapter bluetoothadapter;     private final bluetoothadapter.lescancallback mlescancallback;      private volatile boolean isscanning = false;      scanner(bluetoothadapter adapter, bluetoothadapter.lescancallback callback) {         bluetoothadapter = adapter;         mlescancallback = callback;     }      public boolean isscanning() {         return isscanning;     }      public void startscanning() {         synchronized (this) {             isscanning = true;             start();         }     }      public void stopscanning() {         synchronized (this) {             isscanning = false;             bluetoothadapter.stoplescan(mlescancallback);         }     }      @override     public void run() {         try {             while (true) {                 synchronized (this) {                     if (!isscanning)                         break;                      bluetoothadapter.startlescan(mlescancallback);                 }                  sleep(scan_period);                  synchronized (this) {                     bluetoothadapter.stoplescan(mlescancallback);                 }             }         } catch (interruptedexception ignore) {         } {             bluetoothadapter.stoplescan(mlescancallback);         }     } }   public class save implements serializable {       /**  *   */  private bledevicesadapter ledevicelistadapter; private static final long serialversionuid = 1l;  save(bledevicesadapter blelist) {      ledevicelistadapter = blelist;  }  public void savedata() throws filenotfoundexception{      string filename = "file.txt";      fileoutputstream fos = null;     //bundle extras = getintent().getextras();     //long timestamp = extras.getlong("currenttime");     try {     fos= openfileoutput(filename, context.mode_private);     objectoutputstream out = new objectoutputstream(fos);     //out.write((int) timestamp);     out.writeobject(ledevicelistadapter);     out.close();     toast.maketext(devicescanactivity.this, r.string.list_saved, toast.length_short).show();     } catch (filenotfoundexception e){         e.printstacktrace();     } catch (ioexception e){         e.printstacktrace();     }          }    }  } 

in here when press button, in r.id.menu_stop case, calls class save save list xml file. here logcat when execute app:

new logcat!!!!!:

    03-10 10:33:02.426: d/bluetoothadapter(21891): startlescan(): null     03-10 10:33:02.431: d/bluetoothadapter(21891): onclientregistered() - status=0 clientif=4     03-10 10:33:02.441: d/abslistview(21891): unregisterirlistener() called      03-10 10:33:02.466: d/abslistview(21891): unregisterirlistener() called      03-10 10:33:02.486: d/bluetoothadapter(21891): stoplescan()     03-10 10:33:02.521: w/system.err(21891): java.io.notserializableexception: com.example.newblescan.adapter.bledevicesadapter     03-10 10:33:02.521: w/system.err(21891):    @ java.io.objectoutputstream.writenewobject(objectoutputstream.java:1364)     03-10 10:33:02.521: w/system.err(21891):    @ java.io.objectoutputstream.writeobjectinternal(objectoutputstream.java:1671)     03-10 10:33:02.521: w/system.err(21891):    @ java.io.objectoutputstream.writeobject(objectoutputstream.java:1517)     03-10 10:33:02.521: w/system.err(21891):    @ java.io.objectoutputstream.writeobject(objectoutputstream.java:1481)     03-10 10:33:02.521: w/system.err(21891):    @ com.example.newblescan.devicescanactivity$save.savedata(devicescanactivity.java:295)     03-10 10:33:02.521: w/system.err(21891):    @ com.example.newblescan.devicescanactivity.onoptionsitemselected(devicescanactivity.java:116)     03-10 10:33:02.521: w/system.err(21891):    @ android.app.activity.onmenuitemselected(activity.java:2640)     03-10 10:33:02.521: w/system.err(21891):    @ com.android.internal.policy.impl.phonewindow.onmenuitemselected(phonewindow.java:1171)     03-10 10:33:02.521: w/system.err(21891):    @ com.android.internal.view.menu.menubuilder.dispatchmenuitemselected(menubuilder.java:735)     03-10 10:33:02.521: w/system.err(21891):    @ com.android.internal.view.menu.menuitemimpl.invoke(menuitemimpl.java:152)     03-10 10:33:02.526: w/system.err(21891):    @ com.android.internal.view.menu.menubuilder.performitemaction(menubuilder.java:874)     03-10 10:33:02.526: w/system.err(21891):    @ com.android.internal.view.menu.actionmenuview.invokeitem(actionmenuview.java:630)     03-10 10:33:02.526: w/system.err(21891):    @ com.android.internal.view.menu.actionmenuitemview.onclick(actionmenuitemview.java:200)     03-10 10:33:02.526: w/system.err(21891):    @ android.view.view.performclick(view.java:4475)     03-10 10:33:02.526: w/system.err(21891):    @ android.view.view$performclick.run(view.java:18786)     03-10 10:33:02.526: w/system.err(21891):    @ android.os.handler.handlecallback(handler.java:730)     03-10 10:33:02.526: w/system.err(21891):    @ android.os.handler.dispatchmessage(handler.java:92)     03-10 10:33:02.526: w/system.err(21891):    @ android.os.looper.loop(looper.java:137)     03-10 10:33:02.526: w/system.err(21891):    @ android.app.activitythread.main(activitythread.java:5493)     03-10 10:33:02.526: w/system.err(21891):    @ java.lang.reflect.method.invokenative(native method)     03-10 10:33:02.526: w/system.err(21891):    @ java.lang.reflect.method.invoke(method.java:525)     03-10 10:33:02.526: w/system.err(21891):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1209)     03-10 10:33:02.526: w/system.err(21891):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1025)     03-10 10:33:02.531: w/system.err(21891):    @ dalvik.system.nativestart.main(native method)     03-10 10:33:02.931: d/bluetoothadapter(21891): stoplescan()     03-10 10:33:02.931: d/bluetoothadapter(21891): stoplescan()     03-10 10:33:07.701: d/abslistview(21891): unregisterirlistener() called      03-10 10:33:07.911: d/abslistview(21891): ondetachedfromwindow 

can solve error or put correction of code ot code might missing?? pleaseee help!!!!!!!

update: when press button stop scan, stops don´t know why doesn´t save or create xml file. also, doesn´t pop toast knowing has been saved. updated code above , logcat. can solve new problems??!!!!!!

in savedata()

toast.maketext(null, r.string.list_saved, toast.length_short).show(); 

passing null in context ! ?

that should be,

toast.maketext(devicescanactivity.this, r.string.list_saved, toast.length_short).show(); 

also, initialise fileoutputstream variable,

fileoutputstream fos = null; 

instead of

fileoutputstream fos; 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -