android Null pointer issue in livewallpaper unsure of the cause
I'm writing a livewallpaper that will pull images from flickr and make them the background, and change every so often. Right now i've stubbed/ignored/circumvented the flickr part, and am ignoring the timed changes as well. Still i'm having the same null pointer error over and over and i can't figure out what's causing it.
here is the few methods related to the err开发者_如何学Cor
@Override
public void onVisibilityChanged(boolean visible) {
mVisible = visible;
if (visible) {
setImage(getPic());//***NULLPOINTERERROR
} else {
mHandler.removeCallbacks(getFlickrPic);
}
}
public void setImage(Bitmap bm)
{
final SurfaceHolder holder = getSurfaceHolder();
Canvas c= null;
try{
c=holder.lockCanvas();
if (c != null) {
c.setBitmap(bm);
}
} finally { if (c != null) holder.unlockCanvasAndPost(c); }
}
public Bitmap getPic()
{
Bitmap bm = null;
bm = ((BitmapDrawable) LoadImageFromWebOperations("http://farm6.static.flickr.com/5102/5655314644_b7038a5438_z.jpg")).getBitmap();
while (bm==null)
{bm = ((BitmapDrawable) getResources().getDrawable(R.drawable.no)).getBitmap();}
return bm;
}
private Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).openStream();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
I keep getting a NullPointerException for method getPic() in the OnVisibilityChanged(visible) method, noted with the *comment.
If any more information is necessary please ask. like i said i don't know why it's giving me this error so there's a possibility it's something from code i haven't included. thanks!
Wow thats that for a construct?
while (bm==null) {
bm = ((BitmapDrawable) getResources().getDrawable(R.drawable.no)).getBitmap();
}
That looks evil...
Anyway, your issue is the following line:
bm = ((BitmapDrawable) LoadImageFromWebOperations("http://farm6.static.flickr.com/5102/5655314644_b7038a5438_z.jpg")).getBitmap()
LoadImageFromWebOperations
can return null. In that case you still call getBitmap()
on a null reference...
LoadImageFromWebOperations returns null if an error occures. in getPic you try to call getBitmap() on the return value of LoadImageFromWebOperations - if the value returned is null you are trying to call getBitmap() on a null-object, which will cause your NPE
精彩评论