I'm taking a course on android app development and trying to create and write an xml file to the internal storage on the android. I am having issues with how to set this up initially, as far as the methods. I've written most of it but have errors that I can't figure out. Maybe because I've been working on this all day, I don't know. Here's my code for this class. Errors I'm getting are illegal modifiers on public String treasures and FileOutputStream. Any help would be appreciated.
Ok, I figured out the initial problem, needed to use try/catch. Was able to run and everything worked fine until I got to the save file. Getting an error now:
SoundPool error loading/system./media./audio./ui/KeypressReturn.ogg.
AudioService Soundpool could not load file: /system/media/audio/ui/KeypressReturnj.ogg
This comes right after the "file created" is written to the log. I'm guessing it's trying to write to the wrong file? Need it to write to /data/data. There is no audio in my app. I've added the new code below:
Old Code:
public void onSaveTreasureClick(View v) throws FileNotFoundException{
Log.v("SaveTreasure","Button was clicked");
File f = new File(getFilesDir(),"treasure.xml");
FileOutputStream myFile=openFileOutput(f);
Log.v("WriteFile","file created");
private FileOutputStream openFileOutput(File f) {
// TODO Auto-generated method stub
return null;
}
public String treasures(Treasure treasure) throws Exception{
XmlSerializer xmlSerializer = Xml.newSerializer();
StringWriter write = new StringWriter();
final EditText tres=(EditText) findViewById(R.id.treasureNametxt);
String treasureName=tres.getText().toString();
final EditText c1=(EditText) findViewById(R.id.clue1Txt);
String clue1=c1.getText().toString();
final EditText c2=(EditText) findViewById(R.id.clue2Txt);
String clue2=c2.getText().toString();
final EditText c3=(EditText) findViewById(R.id.clue3Txt);
String clue3=c3.getText().toString();
final EditText ans=(EditText) findViewById(R.id.answerTxt);
String answer = ans.getText().toString();
final EditText loc =(EditText) findViewById(R.id.locationTxt);
String location = loc.getText().toString();
final EditText pv=(EditText) findViewById(R.id.pointValueTxt);
String pointValue=pv.getText().toString();
xmlSerializer.setOutput(write);
//start Document
xmlSerializer.startDocument("UTF-8",true);
//open tag <items>
xmlSerializer.startTag("", "Items");
xmlSerializer.startTag("", "Treasures");
xmlSerializer.startTag("", "TreasureName");
xmlSerializer.attribute("", TreasureName, treasureName);
xmlSerializer.endTag("", "TreasureName");
xmlSerializer.startTag("", "Clue1");
xmlSerializer.attribute("", "Clue1", clue1);
xmlSerializer.endTag("", "Clue1");
xmlSerializer.startTag("", "Clue2");
xmlSerializer.attribute("", "Clue2", clue2);
xmlSerializer.endTag("", "Clue2");
xmlSerializer.startTag("", "Clue3");
xmlSerializer.attribute("", "Clue3", clue3);
xmlSerializer.endTag("", "Clue3");
xmlSerializer.startTag("", "answer");
xmlSerializer.attribute("", "answer", answer);
xmlSerializer.endTag("","answer");
xmlSerializer.startTag("", "location");
xmlSerializer.attribute("", "location", location);
xmlSerializer.endTag("", "location");
xmlSerializer.startTag("", "Points");
xmlSerializer.attribute("", "PointValue", pointValue);
xmlSerializer.endTag("", "Points");
xmlSerializer.endTag("","Treasures");
xmlSerializer.endTag("", "Items");
xmlSerializer.endDocument();
return treasure.toString();
}
}
}
New Code:
public void onSaveTreasureClick(View v) throws FileNotFoundException, SAXException{
Log.v("SaveTreasure","Button was clicked");
File f = new File(getFilesDir(),"treasure.xml");
FileOutputStream myFile=openFileOutput(f);
Log.v("WriteFile","file created");
// private FileOutputStream openFileOutput(File f) {
// TODO Auto-generated method stub
// return null;
// }
try{
final String treasures;
XmlSerializer xmlSerializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
final EditText tres=(EditText) findViewById(R.id.treasureNametxt);
String treasureName=tres.getText().toString();
final EditText c1=(EditText) findViewById(R.id.clue1Txt);
String clue1=c1.getText().toString();
final EditText c2=(EditText) findViewById(R.id.clue2Txt);
String clue2=c2.getText().toString();
final EditText c3=(EditText) findViewById(R.id.clue3Txt);
String clue3=c3.getText().toString();
final EditText ans=(EditText) findViewById(R.id.answerTxt);
String answer = ans.getText().toString();
final EditText loc =(EditText) findViewById(R.id.locationTxt);
String location = loc.getText().toString();
final EditText pv=(EditText) findViewById(R.id.pointValueTxt);
String pointValue=pv.getText().toString();
xmlSerializer.setOutput(writer);
//start Document
xmlSerializer.startDocument("UTF-8",true);
//open tag <items>
xmlSerializer.startTag("", "Items");
xmlSerializer.startTag("", "Treasures");
xmlSerializer.startTag("", "TreasureName");
xmlSerializer.attribute("", treasureName, treasureName);
xmlSerializer.endTag("", "TreasureName");
xmlSerializer.startTag("", "Clue1");
xmlSerializer.attribute("", "Clue1", clue1);
xmlSerializer.endTag("", "Clue1");
xmlSerializer.startTag("", "Clue2");
xmlSerializer.attribute("", "Clue2", clue2);
xmlSerializer.endTag("", "Clue2");
xmlSerializer.startTag("", "Clue3");
xmlSerializer.attribute("", "Clue3", clue3);
xmlSerializer.endTag("", "Clue3");
xmlSerializer.startTag("", "answer");
xmlSerializer.attribute("", "answer", answer);
xmlSerializer.endTag("","answer");
xmlSerializer.startTag("", "location");
xmlSerializer.attribute("", "location", location);
xmlSerializer.endTag("", "location");
xmlSerializer.startTag("", "Points");
xmlSerializer.attribute("", "PointValue", pointValue);
xmlSerializer.endTag("", "Points");
xmlSerializer.endTag("","Treasures");
xmlSerializer.endTag("", "Items");
xmlSerializer.endDocument();
writer.toString();
myFile.write(writer.toString().getBytes());
}
catch (FileNotFoundException e) {
System.err.println("FileNotFoundException: " + e.getMessage());
throw new SAXException(e);
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
}
}
See Question&Answers more detail:os