我正在制作一个能够逐行读取文本文件的应用程序。我的应用程序中有一些TextView,它们从这些行中获取文本,但是我遇到了一个期望:
Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
这是我的代码:
public File file = new File("/sdcard/datacuaapp.txt");
@Override
protected void onCreate(Bundle savedInstanceState) {
checkQuyen();
dong[0]="";
dong[1]="";
dong[2]="";
dong[3]="";
TextView tv1=(TextView) findViewById(R.id.so1);
TextView tv2=(TextView) findViewById(R.id.so2);
TextView tv3=(TextView) findViewById(R.id.so3);
TextView tv4=(TextView) findViewById(R.id.so4);
if (!file.exists())
{
try {
FileWriter writer = new FileWriter(file);
writer.append("@" + "\n" + "@" + "\n" + "@" + "\n" + "@" + "\n" );
writer.flush();
writer.close();
}
catch(IOException io)
{
io.printStackTrace();
}
}
else{
doc(); // the problem is in these lines
tv1.setText(dong[0]);
tv2.setText(dong[1]);
tv3.setText(dong[2]);
tv4.setText(dong[3]);
}
}
public String[] dong = new String[4];
public void doc()
{
Integer a = 0;
try {
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line=bufferedReader.readLine();
while (a<4) {
stringBuffer.append(line);
stringBuffer.append("\n");
dong[a]=line;
a++;
}
fileReader.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}