我试图通过读取文件在 EditText 中设置文本,但应用程序每次都会关闭。有人能说出这段代码有什么问题吗?
package com.example;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class EditNoteActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
String FILENAME = "note_file";
EditText text = (EditText) findViewById(R.id.editText1);
byte[] buffer = new byte[100];
super.onCreate(savedInstanceState);
setContentView(R.layout.editnote);
//Intent intent = getIntent();
FileInputStream fos = null;
try {
fos = openFileInput(FILENAME);
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
assert fos != null;
try {
fos.read(buffer, 0, 10);
String str = buffer.toString();
text.setTextSize(48);
text.setText(str);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
public void onClickSave(View theButton) {
//Intent intent = new Intent(this, MyActivity.class);
//startActivity(intent);
String FILENAME = "note_file";
EditText text = (EditText) findViewById(R.id.editText1);
FileOutputStream fos = null;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
assert fos != null;
try {
fos.write(text.getText().toString().getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
finish();
}
public void onClickBack(View theButton) {
//Intent intent = new Intent(this, MyActivity.class);
//startActivity(intent);
finish();
}
}
我试图编辑它以删除不相关的代码,但出现错误,“您的帖子没有太多上下文来解释代码部分;请更清楚地解释您的场景。”。不幸的是,没有多少,无论如何这个问题已经得到了回答。