0

我收到一个错误:打开跟踪文件时出错:没有这样的文件或目录 java.net.malformedURLexception:找不到协议:cars.xml

我想在内部存储中创建一个 xml 文件并在列表视图中显示 car_name 和 car_model。

我的代码:

public class MainActivity extends Activity {

ListView listView;
Button createCar;
String XML_CONTENT = "<?xml version=\"1.0\" encoding=\"utf-8\"?><cars><car><carMake></carMake><carModel></carModel><fuelType></fuelType><carYear></carYear><carColor></carColor><engineHp></engineHp><engineCcm></engineCcm></car></cars>";
static String myXML = "cars.xml";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] myCars = null;
    fileCreate();

    ParseXML parseXML= new ParseXML(); 
    myCars = parseXML.giveCars(myXML);

    listView = (ListView) findViewById(R.id.listView1);
    if (myCars.length == 0 || myCars == null) 
            Toast.makeText(this, "Your car list is empty. Please create a new car!", 8000).show();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_view_row, R.id.textView1, myCars);
    listView.setAdapter(adapter);
}
}

private void fileCreate() {

    try {
        FileOutputStream os = openFileOutput(myXML, MODE_PRIVATE);
        os.write(XML_CONTENT.getBytes());
        os.close();
        Toast.makeText(MainActivity.this, "created", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(MainActivity.this, "NON created", Toast.LENGTH_LONG).show();
        Log.i("ReadNWrite, fileCreate()", "Exception e = " + e);
    }
}

giveCars 函数:

public String[] giveCars(String cars) {

    try {

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(cars);

        String[] carsInFile = null;
        NodeList myCars = doc.getElementsByTagName("car");
        Node myCar;
        NodeList spec;

        for (int i = 0; i < myCars.getLength(); i++) {
            myCar = myCars.item(i);
            spec = myCar.getChildNodes();
            carsInFile[i] = spec.item(0).toString() + " "
                    + spec.item(1).toString();
        }
        return carsInFile;
    } catch (Exception e) {
        android.util.Log.e("tag", "", e);
    }
    return null;
}

如果在一个简单的 java 项目中运行这个函数,一切正常。

4

1 回答 1

0

您的 openFileOutput 函数可能有错误,但我不知道为什么。这是使它变得更好的提示。您可以保存对象汽车的数组,而不是保存 xml 文件。它优化了您的代码。看看这个函数(对象需要是可序列化的)

public static void saveFile(String filename, Object object) {
        try {
            FileOutputStream fos = AplicationHelper.getAppContext()
                    .openFileOutput(filename, Context.MODE_PRIVATE);
            ObjectOutputStream os = new ObjectOutputStream(fos);
            os.writeObject(object);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
}

并加载它,你有这个功能:

public static Object loadFile(String filename) {
    Object object = null;
    try {
        Log.i("AppData.Load", "Trying to load file " + filename);
        FileInputStream fis = ApplicationHelper.getAppContext()
                .openFileInput(filename);
        ObjectInputStream is = new ObjectInputStream(fis);
        object = is.readObject();
        is.close();
    } catch (Exception e) {
        Log.i("AppData.Load", "Exception " + e);
        return null;
    } catch (NoClassDefFoundError e) {
        return null; // when on desktop mode
    }
    return object;
}

您可以轻松地将文件中的数组直接加载到您的arrayadapter

于 2013-11-01T20:54:24.517 回答