我正在关注本教程:http://www.codelearn.org/android-tutorial/twitter/10/asynctask-parameters-example
但使用我自己的代码(不做他们的Twitter应用程序)
我似乎无法使openFileOutput工作并尝试将此传递作为参考的组合。
我的类myList通过
调用AsyncFetchData new AsyncFetchData().execute(this);
我试图用
打开AsyncFetchData中的文件输出fos = this.openFileOutput(TEA_CACHE_FILE, MODE_PRIVATE);
正如他们在教程中所说的那样。我不能让它工作。至于其余的代码,我只是想创建一个包含虚拟数据的列表,等待是模拟网络调用的延迟。感谢您的帮助,这是我的代码
公共类AsyncFetchData扩展AsyncTask<列表与LT; TeaDetails>,Void,Void> {
private static final String TEA_CACHE_FILE = "tea_cache.ser";
private List<TeaDetails> tempTea = new ArrayList<TeaDetails>();
@Override
protected Void doInBackground(List < TeaDetails > ... params) {
//while the long job getting done {
//update progress_data
//update result
//}
try {
Thread.sleep(5000);
// create dummy tea list
}
catch (Exception e){}
for (int i=1; i<10; i++){
TeaDetails tea = new TeaDetails();
tea.setId("Id is: " + i);
tea.setTitle("Title for tea number " + i);
tea.setInfo("Some information about tea "+ i);
tea.setImgSrc(i);
tempTea.add(tea);
}
FileOutputStream fos;
ObjectOutputStream oos;
try {
//code to write into file
fos = this.openFileOutput(TEA_CACHE_FILE, MODE_PRIVATE);
oos = new ObjectOutputStream(fos);
oos.writeObject(tempTea);
// show in log that file was successfully written
Log.d("HelloWorld2", "Successfully wrote teas to the file.");
//close operators
oos.close();
fos.close();
} catch (Exception e) {
Log.e("HelloWorld2", "Error writing tweets", e);
}
return null;
}
protected void onProgressUpdate(Void... progress_data) {
//use progress_data to show progress on the screen
}
protected void onPostExecute(Void... result) {
//use result obtained at the end of
}
}
答案 0 :(得分:0)
您正在尝试来自openFileOutput
的{{1}}来电,AsyncTask
不是Context
。而不是
this.openFileOutput(...);
你应该
YourActivity.this.openFileOutput(...);
假设这是一个内部类,否则this
引用您的AsyncTask
而不是封闭活动。