0
private class AsyncCaller extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
//this method will be running on background thread so don't update UI from here
//do your long running http tasks here,you don't want to pass argument and u can access the parent class' variable url over here
new DummyContent().fetchData();
return null;
}
}
それは私が How to use AsyncTask correctly in Androidと同様にAsyncTaskを試した後Failed to call a dynamic method in a static context?としてAsyncTaskでListFragmentを更新していますか?
public static void fetchData() {
// Add some sample items.
for (int i = 1; i <= COUNT; i++) {
addItem(createDummyItem(i));
}
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
DummyContent.UNAME, DummyContent.PASSWORD.toCharArray());
}
});
try {
DummyContentRegistry registry = new XMLDummyContentParser(url).parse();
for (DummyItem t: registry.getTeachers()) {
addItem(t);
}
} catch (Exception e) {
fail("Exception should not have been thrown");
}
}
基本的に同じ質問を「Excpetionがスローされていないはず」ランタイムエラーがスローされます。
07-25 05:44:12.080 21933-22106/********.myapplication E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
Process: ***********.myapplication, PID: 21933
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.AssertionError: Exception should not have been thrown
at org.junit.Assert.fail(Assert.java:88)
at ***********.myapplication.dummy.DummyContent.fetchData(DummyContent.java:58)
at ***********.myapplication.MainActivity$AsyncCaller.doInBackground(MainActivity.java:205)
at ***********.myapplication.MainActivity$AsyncCaller.doInBackground(MainActivity.java:200)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)