-1

我正在使用图像解析器从 html 中获取图像 .. 有时它会导致应用程序被挂起并强制停止并抛出 nullpointerexception .. 我不知道这是什么原因但是当我点击 logcat 中的错误时,指针会指向这个线

urlDrawable.setBounds(0, 0, 0+result.getIntrinsicWidth(), 0+result.getIntrinsicHeight()); 

这是我的所有代码:

package com.engahmedphp.facebookcollector;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.Html.ImageGetter;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class URLImageParser implements ImageGetter {
    Context c;
    TextView container;
    TextView textView;
    InputStream is;

    /***
     * Construct the URLImageParser which will execute AsyncTask and refresh the
     * container
     * 
     * @param t
     * @param c
     */
    public URLImageParser(View t, Context c) {
        this.c = c;
        this.container = (TextView) t;
        this.textView = (TextView) t;
    }

    public Drawable getDrawable(String source) {
        URLDrawable urlDrawable = new URLDrawable();

        // get the actual source
        ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask(urlDrawable);

        asyncTask.execute(source);

        // return reference to URLDrawable where I will change with actual image
        // from
        // the src tag
        return urlDrawable;
    }

    public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
        URLDrawable urlDrawable;

        public ImageGetterAsyncTask(URLDrawable d) {
            this.urlDrawable = d;
        }

        @Override
        protected Drawable doInBackground(String... params) {
            String source = params[0];
            return fetchDrawable(source);
        }

        @Override
        protected void onPostExecute(Drawable result) {
            // set the correct bound according to the result from HTTP call 
//          Log.d("height",""+result.getIntrinsicHeight()); 
//          Log.d("width",""+result.getIntrinsicWidth()); 
            urlDrawable.setBounds(0, 0, 0+result.getIntrinsicWidth(), 0+result.getIntrinsicHeight());  

            // change the reference of the current drawable to the result 
            // from the HTTP call 
            urlDrawable.drawable = result; 

            // redraw the image by invalidating the container 
            URLImageParser.this.container.invalidate();

            // For ICS
            URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()));

            // Pre ICS
//          URLImageParser.this.textView.setEllipsize(null);
        }

        /***
         * Get the Drawable from URL
         * 
         * @param urlString
         * @return
         */
        public Drawable fetchDrawable(String urlString) {
            try {

                Drawable drawable = Drawable.createFromStream(fetch(urlString), "src");
                drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0 + drawable.getIntrinsicHeight());
                 drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0
                 + drawable.getIntrinsicHeight());
                return drawable;
            } catch (Exception e) {
                return null;
            }
        }

        private InputStream fetch(String urlString) throws MalformedURLException, IOException {

            try {
                URL newURL = new URL(urlString);
                HttpURLConnection con = (HttpURLConnection) newURL.openConnection();
                is = con.getInputStream();

            } catch (MalformedURLException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            return is;

        }
    }
}
4

1 回答 1

0

你在这里返回 null

 public Drawable fetchDrawable(String urlString) {
        try {

            Drawable drawable = Drawable.createFromStream(fetch(urlString), "src");
            drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0 + drawable.getIntrinsicHeight());
             drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0
             + drawable.getIntrinsicHeight());
            return drawable;
        } catch (Exception e) {
            return null;
        }
    }

因此,请在对结果进行任何操作之前检查if(result != null)并尝试记录您的咳嗽异常以更具体地处理它们。

Drawable fetchDrawable(String urlString){
 try {
   ...
 }catch(Exceptio e){
    e.printStackTrace();
 }
}
于 2013-10-07T20:55:33.087 回答