【发布时间】:2013-08-29 09:46:11
【问题描述】:
我想在自定义视图中创建 scalebitmap,当我想添加 xml 布局时,xml 给出错误java.lang.NullPointerException 并且我想更改一些行,包括我 init() 中 onSizeChangedFunction() 中的 scalebitmap .我的代码如下;
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
parentWidth=w;
parentHeight=h;
init(); // <--- This
mBitmap=Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
还有我的 init() 函数:
private void init()
{
for(int i=0 ; i<2; i++)
{
int imageResources;
imageResources=getResources().getIdentifier("rakam"+i, "drawable", myContext.getPackageName());
Bitmap tempBitmap = BitmapFactory.decodeResource(myContext.getResources(), imageResources);
--> Bitmap scaledBitmap= Bitmap.createScaledBitmap(tempBitmap, parentWidth, parentHeight, false);
rakamlar.add(scaledBitmap);
}
}
错误不在 Logcat 中,它在 eclipse 的 xml 编辑器中。 我该如何解决这个问题?
【问题讨论】:
-
rakam0 和 rakam1 都是可绘制对象吗?
-
空指针很容易找到。使用调试来查看实际为空的内容,然后从那里修复它。如果某些方法返回 null 并且您无法弄清楚原因,请返回更详细的问题。
-
发布您的 logcat 输出可能会使这个问题更清楚。
-
错误不在 logCat 中,而在 eclipce 的 xml 编辑器中。
标签: android android-layout nullpointerexception android-xml android-custom-view