私は奇妙な問題を抱えており、何か間違ったことをしているのか、それとも何かをしているのかわかりません。何らかの理由で、私の AsyncTask は doInBackground を呼び出さず、onPostExecute() を呼び出します。この AsyncTask が呼び出されることが不可欠です。これは、アプリの残りの部分で必要になる変数を初期化するためです。
メイン アクティビティに、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() というメソッドを呼び出します。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() が呼び出されている理由は 2 つあります。LogCat は「Calling refresh」をログに記録します。その後、NullPointerException が原因でアプリがクラッシュします。theEntries が実際に null であるため、null ポインター例外がスローされ、doInBackground() が呼び出されない限り null になります。また、null ポインターが原因で doInBackground が呼び出されることはなく、本体に配置したログが呼び出されることはないこともわかっています。では、doInBackground() メソッドが呼び出されない原因は何でしょうか? 問題があれば、onResume メソッドで AsyncTask を実行し、onCreate または onStart で他の AsyncTask を実行しません。