不完全确定我在这里做错了什么,但我正在尝试使用AsynkTask从https网址下载图像,但我不知何故失败?!为什么我得到空指针异常?
我已经抓住了网址并显示了它,所以我知道它是合法的,我甚至用网络浏览器点击它。
public class WelcomeFragment extends Fragment {
SimpleCursorAdapter adapter;
static final String[] FROM = { StatusData.KEY_USER_NAME };
static final int[] TO = { R.id.txtName };
private ProgressDialog simpleWaitDialog;
private ImageView downloadedImg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
WebView mWebView;
TextView mName, memail, mother, mtextView1;
ImageView mpicture;
View view = inflater.inflate(R.layout.fragment_welcome, null);
String about = getResources().getString(R.string.about_msg);
mName = (TextView) view.findViewById(R.id.txtName);
memail = (TextView) view.findViewById(R.id.txtEmail);
mpicture = (ImageView) view.findViewById(R.id.profilepicture);
mother = (TextView) view.findViewById(R.id.txtOther);
mtextView1 = (TextView) view.findViewById(R.id.textView1);
about = about.replace("\\", "");
about = "<html><body>"
+ "<p align=\"justify\">"
+ about + "</p> "
+ "</body></html>";
// Get the data
Cursor c = getActivity().getContentResolver().query(StatusProvider.CONTENT_URI_USER, null, null, null, null);
if(c.moveToFirst())
{
mName.setText(c.getString(2));
memail.setText(c.getString(5)); //email
mtextView1.setText(c.getString(6)); //picture url
mpicture.setTag(c.getString(6));
new ImageDownloader().execute(c.getString(6));
mother.setText(c.getString(7)); //significant_other
Log.i("WELCOME", "NAME FOUND IN TABLE" );
} else {
Log.i("WELCOME", "NAME NOT FOUND IN TABLE" );
}
mWebView = ((WebView) view.findViewById(R.id.about));
mWebView.loadData(about, "text/html", "utf-8");
return view;
}
private class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... param) {
// TODO Auto-generated method stub
return downloadBitmap(param[0]);
}
@Override
protected void onPreExecute() {
Log.i("Async-Example", "onPreExecute Called");
simpleWaitDialog = ProgressDialog.show(getActivity(),"Wait", "Downloading Image");
}
@Override
protected void onPostExecute(Bitmap result) {
Log.i("Async-Example", "onPostExecute Called");
downloadedImg.setImageBitmap(result);
simpleWaitDialog.dismiss();
}
private Bitmap downloadBitmap(String url) {
// initilize the default HTTP client object
final DefaultHttpClient client = new DefaultHttpClient();
//forming a HttoGet request
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
//check 200 OK for success
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
// getting contents from the stream
inputStream = entity.getContent();
// decoding stream data back into image Bitmap that android understands
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
// You Could provide a more explicit error message for IOException
getRequest.abort();
Log.e("ImageDownloader", "Something went wrong while retrieving bitmap from " + url + e.toString());
}
return null;
}
}
}
logcat的:
05-23 13:32:00.116: E/AndroidRuntime(22911): FATAL EXCEPTION: main
05-23 13:32:00.116: E/AndroidRuntime(22911): java.lang.NullPointerException
05-23 13:32:00.116: E/AndroidRuntime(22911): at com.projectcaruso.naturalfamilyplaning.WelcomeFragment$ImageDownloader.onPostExecute(WelcomeFragment.java:111)
05-23 13:32:00.116: E/AndroidRuntime(22911): at com.projectcaruso.naturalfamilyplaning.WelcomeFragment$ImageDownloader.onPostExecute(WelcomeFragment.java:1)
05-23 13:32:00.116: E/AndroidRuntime(22911): at android.os.AsyncTask.finish(AsyncTask.java:631)
05-23 13:32:00.116: E/AndroidRuntime(22911): at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-23 13:32:00.116: E/AndroidRuntime(22911): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
05-23 13:32:00.116: E/AndroidRuntime(22911): at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 13:32:00.116: E/AndroidRuntime(22911): at android.os.Looper.loop(Looper.java:137)
05-23 13:32:00.116: E/AndroidRuntime(22911): at android.app.ActivityThread.main(ActivityThread.java:4950)
05-23 13:32:00.116: E/AndroidRuntime(22911): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 13:32:00.116: E/AndroidRuntime(22911): at java.lang.reflect.Method.invoke(Method.java:511)
05-23 13:32:00.116: E/AndroidRuntime(22911): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
05-23 13:32:00.116: E/AndroidRuntime(22911): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
05-23 13:32:00.116: E/AndroidRuntime(22911): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
downloadedImg
未初始化