我必须修复一个错误 nullpointerexception
Posted
技术标签:
【中文标题】我必须修复一个错误 nullpointerexception【英文标题】:I have to fix a bug nullpointerexception 【发布时间】:2013-01-31 18:43:30 【问题描述】:我有一个仅出现在 froyo 设备中的错误
Bitmap.createScaledBitmap() 中的空指针异常
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.NullPointerException
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:344)
at com.varxstudio.beautifulwallpapersLite.Imagen.a(Unknown Source)
at com.varxstudio.beautifulwallpapersLite.g.a(Unknown Source)
at com.varxstudio.beautifulwallpapersLite.g.doInBackground(Unknown Source)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
这里的代码有什么解决方案吗?为什么只在froyo??? 以前只使用 BitmapFactory.decodeStream(inputStream) 来加载图像,但我找到了一个flusedInputStream方法来做到这一点,但我仍然得到同样的错误
private class DownloadDialog extends AsyncTask<Void, Void, Void>
ProgressDialog myDialog = null;
int result = 0;
@Override
protected void onPreExecute()
myDialog = ProgressDialog.show(Imagen.this, "", advert);
myDialog.setCancelable(true);
return;
@Override
protected Void doInBackground(Void... params)
result = setWallpaper(path);
return null;
@Override
protected void onPostExecute(Void result)
myDialog.dismiss();
switch (result)
----
return;
public int setWallpaper(String path)
int width, height;
Bitmap dbm, bm;
bm = null;
dbm = null;
InputStream is = null;
WallpaperManager wpm = WallpaperManager.getInstance(this);
dis = getWindowManager().getDefaultDisplay();
if((wpm != null) && (dis != null))
height = dis.getHeight();
width = (int) (height * 1.33);
try
URLConnection conn = new URL(path).openConnection();
conn.connect();
is = conn.getInputStream();
if (is != null)
bm = BitmapFactory.decodeStream(new FlushedInputStream(is));
dbm = Bitmap.createScaledBitmap(bm, width, height, false);
wpm.setBitmap(dbm);
else
return 2;
catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
finally
if (is != null)
try
is.close();
catch (IOException e)
e.printStackTrace();
if(bm != null)
bm.recycle();
if(dbm != null)
dbm.recycle();
else
return 1;
return 0;
static class FlushedInputStream extends FilterInputStream
public FlushedInputStream(InputStream inputStream)
super(inputStream);
@Override
public long skip(long n) throws IOException
long totalBytesSkipped = 0L;
while (totalBytesSkipped < n)
long bytesSkipped = in.skip(n - totalBytesSkipped);
if (bytesSkipped == 0L)
int b = read();
if (b < 0)
break; // we reached EOF
else
bytesSkipped = 1; // we read one byte
totalBytesSkipped += bytesSkipped;
return totalBytesSkipped;
【问题讨论】:
bm
是否为空?如果无法处理图像,decodeStream
返回 null。
【参考方案1】:
你有 NullPointerException 因为
BitmapFactory.decodeStream(new FlushedInputStream(is));
返回 null,BitmapFactory.decodeStream(stream)
返回 null 的原因是因为图像不好..
从某种意义上说,坏图像确保您返回的是 Android 支持的位图格式(例如 Android doesn't support tiff
)。第二个原因是因为图像分辨率,如果图像分辨率太高解码失败..所以确保你有小图像。
这是我在 SO 中发布的关于同一问题的问题..Bitmap failed to create using BitmapFactory.decodeArray
我之前也遇到过同样的问题,但是我通过使用Android开发者网站Displaying Bitmaps Efficiently提供的教程解决了这个问题
【讨论】:
以上是关于我必须修复一个错误 nullpointerexception的主要内容,如果未能解决你的问题,请参考以下文章
如何修复 Vagrant 错误:`private_key_path` 文件必须存在:
如何修复 JAVA VS 代码中的实现错误,“必须实现继承的抽象类”