不会调用asyncTask的doInBackground?

时间:2015-06-23 00:09:37

标签: java android android-asynctask

在我的应用程序中,当我调用新的RetrieveFirstThreeArtUrl()。执行()doInBackground没有被调用..有谁知道为什么?这段代码几天前正在运行,所以我不知道发生了什么......

public class RetrieveFirstThreeArtUrl extends AsyncTask<String, Void, Void> {

    static final String APIURL = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=2ead17554acf667f27cf7dfd4c368f15&artist=%s&album=%s";
    static final String APIURL_ARTIST = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&api_key=2ead17554acf667f27cf7dfd4c368f15&artist=%s";


    @Override
    public void onPreExecute() {

        super.onPreExecute();
        Log.v("", "Pre");
    }

    @Override
    public Void doInBackground(String... args) {
        Log.v("", "Background");


        return null;
    }

    @Override
    public void onPostExecute(Void args) {
        list = (ListView) rootView.findViewById(R.id.list);
        adapter = new LiveAdapter(LiveStreamFragment.this.getActivity(), oslist, LiveStreamFragment.this, list);
        list.setAdapter(adapter);

    }


}

1 个答案:

答案 0 :(得分:1)

用此

替换您的代码

在调用RetrieveFirstThreeArtUrl方法之前,写下这两行

static final String APIURL = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=2ead17554acf667f27cf7dfd4c368f15&artist=%s&album=%s";
static final String APIURL_ARTIST = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&api_key=2ead17554acf667f27cf7dfd4c368f15&artist=%s";

除了调用方法RetrieveFirstThreeArtUrl还有一件事,请使用protected代替public

public class RetrieveFirstThreeArtUrl extends AsyncTask<String, Void, Void> {


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Log.v("", "Pre");
    }

    @Override
    protected void onPostExecute(Void result) {
        list = (ListView) rootView.findViewById(R.id.list);
        adapter = new LiveAdapter(LiveStreamFragment.this.getActivity(), oslist, LiveStreamFragment.this, list);
        list.setAdapter(adapter);
    }

    @Override
    protected Void doInBackground(String... params) {
        Log.v("", "Background");
        return null;
    }       
}