-1

少し助けが必要です...保存ボタンをクリックしてスコアを XML ファイルに書き込み、次回アプリを起動したときに保存したスコアを自動的に取得したいと考えています。

スコアを保存するために次のコードを書きましたが、残念ながら機能しません。ファイルはまったく作成されません。

    public void savescore() throws TransformerException, ParserConfigurationException {

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document xmlDoc = docBuilder.newDocument();
    Element scores = xmlDoc.createElement("score");

    Element adityag= xmlDoc.createElement("aditya");
    adityag.appendChild(xmlDoc.createTextNode(adityas.getText().toString()));
    Element ameyg= xmlDoc.createElement("amey");
    ameyg.appendChild(xmlDoc.createTextNode(ameys.getText().toString()));
    Element akshadag= xmlDoc.createElement("akshada");
    akshadag.appendChild(xmlDoc.createTextNode(akshadas.getText().toString()));
    Element anirudhg= xmlDoc.createElement("anirudh");
    anirudhg.appendChild(xmlDoc.createTextNode(anirudhs.getText().toString()));
    Element aashig= xmlDoc.createElement("aashi");
    aashig.appendChild(xmlDoc.createTextNode(aashis.getText().toString()));
    Element dhvanig= xmlDoc.createElement("dhvani");
    dhvanig.appendChild(xmlDoc.createTextNode(dhvanis.getText().toString()));
    Element mering= xmlDoc.createElement("merin");
    mering.appendChild(xmlDoc.createTextNode(merins.getText().toString()));
    Element swapnilg= xmlDoc.createElement("swapnil");
    swapnilg.appendChild(xmlDoc.createTextNode(swapnils.getText().toString()));
    Element architg= xmlDoc.createElement("archit");
    architg.appendChild(xmlDoc.createTextNode(archits.getText().toString()));
    Element shreyag= xmlDoc.createElement("shreya");
    shreyag.appendChild(xmlDoc.createTextNode(shreyas.getText().toString()));

    scores.appendChild(adityag);
    scores.appendChild(ameyg);
    scores.appendChild(akshadag);
    scores.appendChild(anirudhg);
    scores.appendChild(aashig);
    scores.appendChild(dhvanig);
    scores.appendChild(mering);
    scores.appendChild(shreyag);
    scores.appendChild(swapnilg);
    scores.appendChild(architg);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(xmlDoc);
    StreamResult result = new StreamResult(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "userData.xml"));
    transformer.transform(source, result);
}

私のコードで何が問題になっていますか?

編集: @Gabriella によって提案されたアクセス許可を追加した後、空の XML ファイルが作成されるようになりました

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
4

1 に答える 1

0

外部ストレージへの書き込み権限をマニフェスト ファイルに追加します。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

または、この質問を見てください: how to create xml file in android

編集:

空の要素をxmlドキュメントに追加しているため、コードは次のようになるはずです。

public void savescore() throws TransformerException, ParserConfigurationException {

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document xmlDoc = docBuilder.newDocument();
    Element scores = xmlDoc.createElement("score");

    Element adityag= xmlDoc.createElement("name1");
    adityag.appendChild(xmlDoc.createTextNode(name1.getText().toString()));
    Element ameyg= xmlDoc.createElement("name2");
    ameyg.appendChild(xmlDoc.createTextNode(name2.getText().toString()));
    Element akshadag= xmlDoc.createElement("name3");
    akshadag.appendChild(xmlDoc.createTextNode(name3.getText().toString()));
    Element anirudhg= xmlDoc.createElement("name4");
    anirudhg.appendChild(xmlDoc.createTextNode(name4.getText().toString()));
    Element aashig= xmlDoc.createElement("name5");
    aashig.appendChild(xmlDoc.createTextNode(aname5.getText().toString()));
    Element dhvanig= xmlDoc.createElement("name6");
    dhvanig.appendChild(xmlDoc.createTextNode(name6.getText().toString()));
    Element mering= xmlDoc.createElement("name7");
    mering.appendChild(xmlDoc.createTextNode(name7.getText().toString()));
    Element swapnilg= xmlDoc.createElement("name8");
    swapnilg.appendChild(xmlDoc.createTextNode(name8.getText().toString()));
    Element architg= xmlDoc.createElement("name9");
    architg.appendChild(xmlDoc.createTextNode(name9.getText().toString()));
    Element shreyag= xmlDoc.createElement("name10");
    shreyag.appendChild(xmlDoc.createTextNode(name10.getText().toString()));

    scores.appendChild(adityag);
    scores.appendChild(ameyg);
    scores.appendChild(akshadag);
    scores.appendChild(anirudhg);
    scores.appendChild(aashig);
    scores.appendChild(dhvanig);
    scores.appendChild(mering);
    scores.appendChild(swapnilg);
    scores.appendChild(architg);
    scores.appendChild(shreyag);

    xmlDoc.appendChild(scores);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(xmlDoc);
    StreamResult result = new StreamResult(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "RummyScore" + "/" + "userData.xml"));
    transformer.transform(source, result);
}

name1、nam2、name3 などは TextView ですか?

于 2015-07-10T08:42:50.597 に答える