我有一个奇怪的问题,我不知道如果我做错了什么或什么的。 出于某种原因,我的AsyncTask不会调用doInBackground,但会调用onPostExecute()。这个AsyncTask被调用是很重要的,因为它初始化了我在整个应用程序的其余部分需要的变量。AsyncTask的onPostExecute被调用:但doInBackground不是
我在扩展AsyncTask的主要活动中有一个嵌套类。这个类需要从用户的Dropbox帐户下载的文件名护理:
protected class FilesLister extends AsyncTask<String, Integer, Long>
{
@Override
protected Long doInBackground(String... params)
{
Log.i("Entries", "We believe Entries has some values now.");
ArrayList<Entry> theFiles = new ArrayList<Entry>();
try
{
Entry entries = mDBApi.metadata("/", 20000, null, true, null);
for (Entry e : entries.contents)
{
Log.i("Hm", e.fileName());
if (!e.isDeleted)
{
if(!e.isDir)
{
theFiles.add(e);
}
}
}
theEntries = theFiles.toArray(new Entry[theFiles.size()]);
} catch (DropboxException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
protected void onPostExecute(Long result)
{
super.onPostExecute(result);
Log.i("Now", "Calling refresh.");
refresh();
}
}
就像你所看到的,onPostExecute调用一个方法被称为刷新()。 refresh()实现如下:
public void refresh()
{
//Sort all this files by last modified date.
Arrays.sort(theEntries, new Comparator<Entry>()
{
@Override
public int compare(Entry firstFile, Entry secondFile)
{
//"EEE, dd MMM yyyy kk:mm:ss ZZZZZ"
SimpleDateFormat formater = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss ZZZZZ");
try
{
Date date1 = (Date)formater.parse(firstFile.modified);
Date date2 = (Date)formater.parse(secondFile.modified);
return date1.compareTo(date2);
} catch (ParseException e1)
{
e1.printStackTrace();
}
return 0;
}
});
//Now we create a String[] array to hold the names of all the fetched files...
ArrayList<String>txtFilesNames = new ArrayList<String>();
for(int i = theEntries.length - 1; i >= 0; i--)
{
//Loops goes from top to bottom so the latest file appears at the top of the ListView.
txtFilesNames.add(theEntries[i].fileName());
}
actualFileNames = txtFilesNames.toArray(new String[txtFilesNames.size()]);
ArrayAdapter<String> ad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, actualFileNames);
lv.setAdapter(ad);
}
我知道refresh()被调用的原因有两个:LogCat日志“调用刷新”。然后由于NullPointerException导致应用程序崩溃。空指针异常被抛出,因为Entries确实为null,除非调用doInBackground(),否则它将为空。我知道doInBackground从来没有被调用,因为空指针,因为我放在它身上的日志永远不会被调用。那么,什么可能导致我的doInBackground()方法不被调用?如果它很重要,我在我的onResume方法中执行AsyncTask,并且我不会在onCreate或onStart中执行任何其他AsyncTasks。
第一件事,第一,为什么你正在使用的AsyncTask <字符串,整数,长>如果你的doinbackground不打算返回任何值何不的AsyncTask <字符串,整数,太虚> ??? – Akram
在doInBackground中设置一个断点,并使用调试器遍历代码。 – Zambotron