我的代码出了什么问题?每次运行代码时都会出现此错误。尝试下载.txt文件,然后在toast中显示文本。这就是我想要的!
: FATAL EXCEPTION: main Process: nnsoft.nsomuhtest, PID: 18706
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.widget.Toast.<init>(Toast.java:138)
at android.widget.Toast.makeText(Toast.java:385)
代码:
@Override
protected void onPostExecute(String unused) {
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(AnnouncementsDirectory));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(context, "error reading ver file", Toast.LENGTH_LONG).show();
}
}