Android 在内存中保存文件无法正常工作
我试图将列表的元素(由用户通过对话框添加)保存到文件中,以便重新打开时保持不变。由于某种原因,当我第二次向其中添加元素时,它会抛出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是我没有“刷新”我的 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.