1
類
我有一個包含2個函數的類:的NullPointerException在
public class FileHandler extends Activity {
public void writeToFile(){
String fileName = "lastDevice.txt";
try {
FileOutputStream fos = openFileOutput(fileName, MODE_PRIVATE); //Exception thrown here
fos.write("some device id".getBytes());
fos.close();
Toast.makeText(this, "File updated", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String readFromFile(){
try {
String fileName = "lastDevice.txt";
FileInputStream fis = openFileInput(fileName); //Exception thrown here
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String sLine = null;
String data ="";
while ((sLine = br.readLine())!=null) {
data+= sLine;
}
return data;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "FileNotFoundException";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "IOException";
} catch (NullPointerException e){
// TODO Auto-generated catch block
e.printStackTrace();
return "Null Pointer Exception";
}
}
這些功能都從我的主要活動如下調用:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvDevices = (ListView)findViewById(R.id.ListViewDevices);
lastDeviceTxt = (TextView)findViewById(R.id.lastDeviceTxt);
//get last connected device
FileHandler fh = new FileHandler();
String last = fh.readFromFile();
lastDeviceTxt.setText(last);
}
,但我不斷收到來自兩種功能NullPointerException
。
當從我的MainActivity
運行功能(我將它們複製到我的主要活動),他們工作正常。 我在做什麼錯? (請記住,我對android
開發非常陌生)。
*我不斷收到來自兩個函數的NullPointerException * =>拋出異常的行是什麼? – assylias 2013-02-19 14:09:55
請添加錯誤日誌 – Ekonion 2013-02-19 14:09:59
getDateTime gdt = new getDateTime();這是什麼意思 ? – Triode 2013-02-19 14:10:44