我不知道为什么我的代码不会显示在我的编辑文本上,有人可以帮忙吗?我真的需要这个工作。
我在 res\raw 文件夹下创建了我的 xml 作为菜单。我不知道读取文件是否有问题。我希望edittext在运行时/oncreate时显示
public class CreateTreasureActivity extends Activity {
private EditText name;
private EditText clue1;
private EditText clue2;
private EditText clue3;
private EditText answer;
private EditText location;
private EditText point_value;
FileOutputStream myFile;
XmlSerializer serializer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_treasure);
name = (EditText) findViewById(R.id.name);
name.setText("");
clue1 = (EditText) findViewById(R.id.clue1);
clue1.setText("");
clue2 = (EditText) findViewById(R.id.clue2);
clue2.setText("");
clue3 = (EditText) findViewById(R.id.clue3);
clue3.setText("");
answer = (EditText) findViewById(R.id.answer);
answer.setText("");
location = (EditText) findViewById(R.id.location);
location.setText("");
point_value = (EditText) findViewById(R.id.point_value );
point_value .setText("");
// create the initial xml document if it already does not exist
try
{
File filename = new File(getFilesDir(), "treasure.xml");
if(!filename.exists())
{
myFile = openFileOutput("treasure.xml", Activity.MODE_APPEND);
// create a new XmlSerializer objectÂ
serializer = Xml.newSerializer();
// use myFile as your xml serializer and set to UTF-8 encoding
serializer.setOutput(myFile, "UTF-8");
// Write <?xml declaration with encodingÂ
serializer.startDocument(null, Boolean.valueOf(true));
// set indentation optionÂ
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
// start a new top level tag and other tags
serializer.startTag(null, "Treasure");
}
else
{
// use myFile as your xml serializer and set to UTF-8 encoding
myFile = openFileOutput("treasure.xml", Activity.MODE_APPEND);
// create a new XmlSerializer objectÂ
serializer = Xml.newSerializer();
// use myFile as your xml serializer and set to UTF-8 encoding
serializer.setOutput(myFile, "UTF-8");
}
}
catch (Exception e)
{
Log.e("Exception::", e.getMessage());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.create_treasure, menu);
return true;
}
public void onSaveButton(View v)
{
try
{
// start a new top level tag and other tags, for our FINAL project, Contact could be Treasure Item
// we don't need to have nested <Treasure> followed by Items, the format can be similar to the below
// this way, we don't need to add an end tag for outermost element
// create tags and values
serializer.startTag(null, "Items");
serializer.startTag(null, "treasure");
serializer.startTag(null, "name");
serializer.text(name.getText().toString()); // retrieve name entered by user from text field
serializer.endTag(null, "name");
serializer.startTag(null, "clue1");
serializer.text(clue1.getText().toString());
serializer.endTag(null, "clue1");
serializer.startTag(null, "clue2");
serializer.text(clue2.getText().toString());
serializer.endTag(null, "clue2");
serializer.startTag(null, "clue3");
serializer.text(clue3.getText().toString());
serializer.endTag(null, "clue3");
serializer.startTag(null, "answer");
serializer.text(answer.getText().toString());
serializer.endTag(null, "answer");
serializer.startTag(null, "pointValue");
serializer.text(point_value.getText().toString());
serializer.endTag(null, "pointValue");
serializer.endTag(null, "Treasure");
serializer.endTag(null, "Items");
// perform the write by flushing
serializer.flush();
// clear the text fields
name.setText("");
clue1.setText("");
clue2.setText("");
clue3.setText("");
answer.setText("");
location.setText("");
point_value.setText("");
}
catch (Exception e)
{
// use logcat to display errors
Log.e("Exception::", e.getMessage());
}
}
}