将项目从列表保存到Android的xml文件时出现问题

时间:2014-03-19 14:34:35

标签: android xml list

我已经实现了一个能够检测BLE信号并使用适配器将其显示为列表的应用程序。显示BLE设备后,我将每个列表的特征保存到xml文件中。当应用程序检测到设备时,它可以完美保存;我现在的问题是,如果它没有检测到任何信号,或者以另一种方式说,列表中没有项目,当它试图将其保存到xml文件时,应用程序崩溃并关闭。我试图通过检查列表中的项目数来修复它;如果它是0,它执行代码的“else”部分,如果它大于0,它将保存列表,如下所示。然而,它仍然导致同样的问题,我不知道该怎么做。

以下是我用来保存到xml文件中的代码:

public class Save {   

  /**
 * Save the last BLE List in a XML File
 */

private BleDevicesAdapter leDeviceListAdapter;

Save(BleDevicesAdapter BLEList) {

    leDeviceListAdapter = BLEList;

}


public void savedata() throws FileNotFoundException{

    String filename = "BLEDevices.xml";

    long dateutc = System.currentTimeMillis();

    long finaldateutc = 62135596800000L + dateutc;

    double frequency = 1/(0.001*SCAN_PERIOD);

    long period = 200;




    FileOutputStream fos = null;
    File sdCard = Environment.getExternalStorageDirectory();
    File directory = new File(sdCard.getAbsolutePath()+"/Mis archivos");
    directory.mkdirs();
    File file= new File(directory, filename);


    if (leDeviceListAdapter.getDevice(0) != null) {
        int size = leDeviceListAdapter.getCount();
        try {
            fos = new FileOutputStream(file);
            //fos= openFileOutput(filename, Context.MODE_WORLD_READABLE);
            XmlSerializer serializer = Xml.newSerializer();
            serializer.setOutput(fos, "UTF-8");
            serializer.startDocument(null, Boolean.valueOf(true));
            serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

            serializer.startTag("", "root");
            serializer.startTag("", "measurement");
            serializer.attribute("", "time", String.valueOf(finaldateutc));
            //serializer.attribute("", "scan period", String.valueOf(period));


            for(int j = 0 ; j < size ; j++) {
                BluetoothDevice devices = leDeviceListAdapter.getDevice(j);
                Integer signal = leDeviceListAdapter.getRSSI(j);
                serializer.startTag("", "device");
                serializer.attribute("", "rssi", String.valueOf(signal));
                serializer.attribute("", "mac", devices.getAddress());
                serializer.attribute("", "name", devices.getName());


                serializer.endTag("", "device");

            }

            //ObjectOutputStream out = new ObjectOutputStream(fos);
            //out.writeObject(leDeviceListAdapter);
            //out.close();

            serializer.endDocument();
            serializer.flush();
            fos.close();
            Toast.makeText(DeviceScanActivity.this, R.string.list_saved, Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e){
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
    }
    else {
        try {
            fos = new FileOutputStream(file);
            //fos= openFileOutput(filename, Context.MODE_WORLD_READABLE);
            XmlSerializer serializer = Xml.newSerializer();
            serializer.setOutput(fos, "UTF-8");
            serializer.startDocument(null, Boolean.valueOf(true));
            serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

            serializer.startTag("", "root");
            serializer.startTag("", "measurement");
            serializer.attribute("", "time", String.valueOf(finaldateutc));
            serializer.endDocument();
            serializer.flush();
            fos.close();
            Toast.makeText(DeviceScanActivity.this, R.string.list_saved, Toast.LENGTH_SHORT).show();
            } catch (FileNotFoundException e){
                e.printStackTrace();
            } catch (IOException e){
                e.printStackTrace();
            }

        }

    }
}

这也是问题的日志:

03-18 12:51:17.081: D/BluetoothAdapter(18804): stopLeScan()
03-18 12:51:17.086: D/AndroidRuntime(18804): Shutting down VM
03-18 12:51:17.086: W/dalvikvm(18804): threadid=1: thread exiting with uncaught exception (group=0x42027700)
03-18 12:51:17.091: E/AndroidRuntime(18804): FATAL EXCEPTION: main
03-18 12:51:17.091: E/AndroidRuntime(18804): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-18 12:51:17.091: E/AndroidRuntime(18804):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at java.util.ArrayList.get(ArrayList.java:308)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at com.example.newblescan.adapter.BleDevicesAdapter.getDevice(BleDevicesAdapter.java:50)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at com.example.newblescan.DeviceScanActivity$Save.savedata(DeviceScanActivity.java:346)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at com.example.newblescan.DeviceScanActivity.onOptionsItemSelected(DeviceScanActivity.java:131)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at android.app.Activity.onMenuItemSelected(Activity.java:2640)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1171)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:630)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:200)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at android.view.View.performClick(View.java:4475)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at android.view.View$PerformClick.run(View.java:18786)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at android.os.Handler.handleCallback(Handler.java:730)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at android.os.Looper.loop(Looper.java:137)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at android.app.ActivityThread.main(ActivityThread.java:5493)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at java.lang.reflect.Method.invokeNative(Native Method)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at java.lang.reflect.Method.invoke(Method.java:525)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
03-18 12:51:17.091: E/AndroidRuntime(18804):    at dalvik.system.NativeStart.main(Native Method)
03-18 12:51:17.161: D/BluetoothAdapter(18804): stopLeScan()
03-18 12:51:17.161: D/BluetoothAdapter(18804): stopLeScan()

任何人都可以帮我解决这个问题或者说出正确的答案吗?请帮忙!!!!!

1 个答案:

答案 0 :(得分:0)

您收到此错误的原因是因为数组列表中没有数据,大小为0.您可以做的是,您可以在BleDevicesAdapter中检查ArrayList是否为空。

public BluetoothDevice getDevice(int position) {

        if(leDevices == null || leDevices.size() == 0 ) {
            return null;
        }

        return leDevices.get(position);
    }
像这样的事情。然后你可以像下面的代码一样检查它。

if (leDeviceListAdapter.getDevice(0) != null)