帮助在 Android 中存储数据

发布于 11-24 12:15 字数 684 浏览 2 评论 0原文

我有一个按钮,单击时它会存储在其上方的文本表单中写入的所有文本。但是,单击按钮后,应用力将关闭。可能是什么问题?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海似她心2024-12-01 12:15:56

在这些情况下,最好首先查看 DDMS 报告的异常。您能提供 DDMS 报告的异常吗?
编辑数据在哪里?它是 EditText 还是 TextView ?您直接在该行中将字符串转换为文件,而不是创建一个文件对象。

应该是这样的:

Scanner scan = new Scanner(new File(editData.getText()));

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:

Scanner scan = new Scanner(new File(editData.getText()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文