我正在使用图像解析器从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;
}
}
}
答案 0 :(得分: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)
并尝试记录您的cought Exception以更具体地处理它们。
Drawable fetchDrawable(String urlString){
try {
...
}catch(Exceptio e){
e.printStackTrace();
}
}