当我尝试启动它时,我的应用程序崩溃了:
DownloadTask downloadTask = new DownloadTask();
这里是类的代码:
private class DownloadTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... url) {
// For storing data from web service
String data = "";
try{
// Fetching the data from web service
data = downloadUrl(url[0]);
}catch(Exception e){
Log.d("Background Task",e.toString());
}
return data;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
ParserTask parserTask = new ParserTask();
// Invokes the thread for parsing the JSON data
parserTask.execute(result);
}
}
我哪里错了?
您需要将一些值传递给DownloadTask AsyncTask。你调用AsyncTask的方式也是错误的。应该是这样的
DownloadTask downloadTask = new DownloadTask();
downloadTask.execute("SOME VALUE");
由于doInBackground函数需要一些String,因此您认为url [0]变为null。所以你的应用程序崩溃了。