Android 在内存中保存文件无法正常工作

发布于 2024-12-19 13:48:36 字数 1835 浏览 1 评论 0原文

我试图将列表的元素(由用户通过对话框添加)保存到文件中,以便重新打开时保持不变。由于某种原因,当我第二次向其中添加元素时,它会抛出 nullPointerException (第一次效果很好)。这就是我保存的方式:

public void onPause(){
        super.onPause();
        try {
            OutputStreamWriter out = new OutputStreamWriter(openFileOutput(salvare, 0));
            for(int i=0; i<lista.orase.size(); i++){
                out.write(lista.getItem(i)+"\n"); //ia fiecare element al listei si il scrie in fisier urmat de endline;
            }
            out.close();
            System.out.println("ON PAUSE!");
        } catch (Throwable t){

        }
    }

这就是我检索 onCreate 的方式:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lista=new ListaOrase(this);
        buton=(Button)findViewById(R.id.buton);

        setListAdapter(lista);

        try {
            InputStream in= openFileInput(salvare);
            if(in!=null){
                InputStreamReader tmp = new InputStreamReader(in);
                BufferedReader reader = new BufferedReader(tmp);
                String str;
                while(reader.readLine()!=null){
                    str=reader.readLine();
                    lista.add(str);
                        }
                in.close();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

此函数抛出异常(在添加保存功能之前效果很好)。它从对话框获取意图并将元素添加到列表中。

public void onActivityResult (int requestCode, int responseCode, Intent data){
                if(responseCode==1){
            String s=data.getStringExtra("oras");   
            lista.add(s);
            setListAdapter(lista);
        }
    }

它们之间可能存在什么联系,我该如何解决它?

I'm trying to save the elements of a list (added by the user via a dialog box) into a file so that it stays the same when reopened. For some reason it throws a nullPointerException when I add elements to it the second time (first time works nicely). This is how I save:

public void onPause(){
        super.onPause();
        try {
            OutputStreamWriter out = new OutputStreamWriter(openFileOutput(salvare, 0));
            for(int i=0; i<lista.orase.size(); i++){
                out.write(lista.getItem(i)+"\n"); //ia fiecare element al listei si il scrie in fisier urmat de endline;
            }
            out.close();
            System.out.println("ON PAUSE!");
        } catch (Throwable t){

        }
    }

And this is how I retrieve onCreate:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lista=new ListaOrase(this);
        buton=(Button)findViewById(R.id.buton);

        setListAdapter(lista);

        try {
            InputStream in= openFileInput(salvare);
            if(in!=null){
                InputStreamReader tmp = new InputStreamReader(in);
                BufferedReader reader = new BufferedReader(tmp);
                String str;
                while(reader.readLine()!=null){
                    str=reader.readLine();
                    lista.add(str);
                        }
                in.close();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

The exception is thrown by this function (which worked beautifully before adding the save feature). It gets the intent from the dialog box and adds the element to the list.

public void onActivityResult (int requestCode, int responseCode, Intent data){
                if(responseCode==1){
            String s=data.getStringExtra("oras");   
            lista.add(s);
            setListAdapter(lista);
        }
    }

What could possibly be the connection between them and how could I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

離殇 2024-12-26 13:48:36

问题是我没有“刷新”我的 ListAdapter。与notifyDataSetChanged()一起,我按照Chunhui的建议阅读了setListAdapter(lista)。

The problem was that I wasn't "refreshing" my ListAdapter. Along with notifyDataSetChanged() I readded setListAdapter(lista) as Chunhui suggested.

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