启动课程时崩溃

时间:2017-12-19 10:44:58

标签: android class

当我尝试启动它时,我的应用程序崩溃了:

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);
    }
 }

我哪里错了?

1 个答案:

答案 0 :(得分:0)

您需要将一些值传递给 DownloadTask AsyncTask。你调用AsyncTask的方式也是错误的。应该像

DownloadTask downloadTask = new DownloadTask();
downloadTask.execute("SOME VALUE"); 

由于 doInBackground 函数需要一些字符串,因此您认为 url [0] 为空。所以你的应用程序崩溃了。