帮助在 Android 中存储数据
我有一个按钮,单击时它会存储在其上方的文本表单中写入的所有文本。但是,单击按钮后,应用力将关闭。可能是什么问题?
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String FILEOUTPUT = Day;
BufferedWriter bfw;
try {
bfw = new BufferedWriter (new FileWriter(FILEOUTPUT));
Scanner scan = new Scanner((File) editData.getText());
bfw.write(scan.nextLine());
bfw.close();
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Save error", Toast.LENGTH_SHORT);
}
}
});
I have a button which stores whatever text is written in the text form above it when clicked. However, the application force closes when the button is clicked. What could be the problem?
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String FILEOUTPUT = Day;
BufferedWriter bfw;
try {
bfw = new BufferedWriter (new FileWriter(FILEOUTPUT));
Scanner scan = new Scanner((File) editData.getText());
bfw.write(scan.nextLine());
bfw.close();
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Save error", Toast.LENGTH_SHORT);
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

在这些情况下,最好首先查看 DDMS 报告的异常。您能提供 DDMS 报告的异常吗?
编辑数据在哪里?它是 EditText 还是 TextView ?您直接在该行中将字符串转换为文件,而不是创建一个文件对象。
应该是这样的:
It's best to first look at the reported Exception from DDMS in these cases. Can you provide the reported exception from DDMS?
Where is editData? Is it an EditText or a TextView ? You are casting a String to a File directly in that line, instead you should be creating a File object.
should be like: