我是 android 的初学者,对于一个单一课程的项目,我们必须开发 5 个简单的应用程序,然后将它们“组合”成一个。所以你从一个 Activity/ListView 启动每个应用程序。所有应用程序都单独运行正常,但是当从列表视图运行一个应用程序时,我得到以下 * AsyncTask #1 执行 doinBackground() 时发生错误;*
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
reportarray= new ArrayList<String>();
try {
URL url = new URL("http://www.yr.no/sted/Sverige/Kronoberg/V%E4xj%F6/forecast.xml");
AsyncTask task = new WeatherRetriever().execute(url);
} catch (IOException ioe ) {
ioe.printStackTrace();
}
adapter= new Adapter(this);
setListAdapter(adapter);
}
这似乎是一个线程问题,这是我使用的更多代码
private class WeatherRetriever extends AsyncTask<URL, Void, WeatherReport> {
protected WeatherReport doInBackground(URL... urls) {
try {
return WeatherHandler.getWeatherReport(urls[0]);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected void onProgressUpdate(Void... progress) {
}
protected void onPostExecute(WeatherReport result) {
report = result;
runOnUiThread(new Runnable() {
public void run() {
reportarray.add(report.toString());;
for (WeatherForecast forecast : report) {
reportarray.add(forecast.toString());
}
adapter.notifyDataSetChanged();
}
});
PrintReportToConsole();
}
}
}
LOGCAT 输出
09-10 14:51:36.347: E/AndroidRuntime(1411): FATAL EXCEPTION: AsyncTask #1
09-10 14:51:36.347: E/AndroidRuntime(1411): java.lang.RuntimeException: An error occured while executing doInBackground()
09-10 14:51:36.347: E/AndroidRuntime(1411): at android.os.AsyncTask$3.done(AsyncTask.java:299)
09-10 14:51:36.347: E/AndroidRuntime(1411): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
09-10 14:51:36.347: E/AndroidRuntime(1411): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
09-10 14:51:36.347: E/AndroidRuntime(1411): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
09-10 14:51:36.347: E/AndroidRuntime(1411): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-10 14:51:36.347: E/AndroidRuntime(1411): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-10 14:51:36.347: E/AndroidRuntime(1411): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-10 14:51:36.347: E/AndroidRuntime(1411): at java.lang.Thread.run(Thread.java:856)
09-10 14:51:36.347: E/AndroidRuntime(1411): Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
09-10 14:51:36.347: E/AndroidRuntime(1411): at com.example.ass1.VaxjoWeather$WeatherRetriever.doInBackground(VaxjoWeather.java:133)
09-10 14:51:36.347: E/AndroidRuntime(1411): at com.example.ass1.VaxjoWeather$WeatherRetriever.doInBackground(VaxjoWeather.java:1)
09-10 14:51:36.347: E/AndroidRuntime(1411): at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-10 14:51:36.347: E/AndroidRuntime(1411): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-10 14:51:36.347: E/AndroidRuntime(1411): ... 4 more
09-10 14:51:36.347: E/AndroidRuntime(1411): Caused by: java.lang.RuntimeException: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
09-10 14:51:36.347: E/AndroidRuntime(1411): at com.example.ass1.WeatherHandler.getWeatherReport(WeatherHandler.java:54)
09-10 14:51:36.347: E/AndroidRuntime(1411): at com.example.ass1.VaxjoWeather$WeatherRetriever.doInBackground(VaxjoWeather.java:131)
09-10 14:51:36.347: E/AndroidRuntime(1411): ... 7 more
09-10 14:51:36.347: E/AndroidRuntime(1411): Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
09-10 14:51:36.347: E/AndroidRuntime(1411): at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
09-10 14:51:36.347: E/AndroidRuntime(1411): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)