Android asyncTask数组返回null

时间:2012-05-23 23:45:38

标签: java android nullpointerexception android-asynctask

我试图重写一些代码因为android指定你必须将网络任务放在版本4.0中的一个单独的线程上,我有一块代码块工作正常但是因为我尝试将它放入异步任务我现在无法获得它完全可以工作,我设置了我的异步任务来返回一个数组,但它不断返回null,这里有什么明显的东西我没看到吗?

package com.imagetest.second;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;

public class ImagetestActivity extends Activity {
// Elements media;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    String url = "http://www.goal.com/en-ca/news/4196/brazil/2012/05/23/3123112/ganso-set-for-knee-surgery";

    new getMainImg().execute(url);

}

private class getMainImg extends AsyncTask<String, Void, Document> {

    @Override
    protected Document doInBackground(String... params) {
        Document doc = null;

        try {
            doc = Jsoup.connect(params[0]).get();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return doc;
    }

    @Override
    protected void onPostExecute(Document result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        List<Element> media = new ArrayList<Element>();
        media = result.select("img[src]");

        List<Dog> theImportantUrlArray = new ArrayList<Dog>();
        for (Element src : media) {
            String srcStr = src.attr("src");
            String first7st = srcStr.substring(0, 7);
            if (first7st.equals("http://")) {

                Integer theHeight = getBitmapFromURL(srcStr).getHeight();
                theImportantUrlArray.add(new Dog(srcStr, theHeight));

            }

        }
        Collections.sort(theImportantUrlArray, new Dog());

        Integer thetweetpic = theImportantUrlArray.get(0).getDogAge();
        String theTweetUrl = theImportantUrlArray.get(0).getDogName();

        Toast.makeText(ImagetestActivity.this,
                thetweetpic.toString() + " " + theTweetUrl,
                Toast.LENGTH_LONG).show();
    }

}

public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

public class Dog implements Comparator<Dog>, Comparable<Dog> {
    private String theUrl;
    private int height;

    Dog() {
    }

    Dog(String n, int a) {
        theUrl = n;
        height = a;
    }

    public String getDogName() {
        return theUrl;
    }

    public int getDogAge() {
        return height;
    }

    // Overriding the compareTo method
    public int compareTo(Dog d) {
        return (this.theUrl).compareTo(d.theUrl);
    }

    // Overriding the compare method to sort the age
    public int compare(Dog d, Dog d1) {
        return d1.height - d.height;
    }
}

}

0 个答案:

没有答案