[英]Android Bitmap becomes null
在我的可繪制對象類中,我有一個成員變量,用於存儲一個位圖,該位圖將被分配給構造函數,當調用render函數時,該位圖已變為null
,我不知道為什么。
類成員和構造函數:
public class MyDrawableObject {
private int mFileLocation;
private final int mId;
private Context mContext;
private Bitmap bmp;
private int X;
private int Y;
private int W;
private int H;
public static List<MyDrawableObject> ObjectList = new ArrayList<MyDrawableObject>();
public MyDrawableObject(int fileloc, Context con) {
mFileLocation = fileloc;
mId = ObjectList.size();
mContext = con;
ObjectList.add(this);
Bitmap bmp = BitmapFactory.decodeResource(mContext.getResources(), mFileLocation);
// Store width and height
W = bmp.getWidth();
H = bmp.getHeight();
Log.d("DrawableObject", "Width: " + W + " Height: " + H);
Log.d("DrawableObject", "Object Added to list, ID: " + mId);
Log.d("DrawableObject", "ID: " + mId + " Filelocation: " + mFileLocation);
}
發生錯誤的功能
public void SetupImage(Context mContext) {
// Create our UV coordinates.
float[] uvs = new float[] {
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f
};
// The texture buffer
ByteBuffer bb = ByteBuffer.allocateDirect(uvs.length * 4);
bb.order(ByteOrder.nativeOrder());
uvBuffer = bb.asFloatBuffer();
uvBuffer.put(uvs);
uvBuffer.position(0);
// Generate Textures, if more needed, alter these numbers.
int[] texturenames = new int[1];
GLES20.glGenTextures(1, texturenames, 0);
// Bind texture to texture name
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texturenames[0]);
// Set filtering
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
if(bmp == null) {
Log.d("DrawableObject", "NULL BITMAP");
} else {
Log.d("DrawableObject", "NON NULL");
}
// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bmp, 0);
}
運行此命令時,我在日志中收到"NULL BITMAP"
消息,並且應用程序崩潰。 除了上面在構造函數和上面的SetupImage
函數中的兩個示例以外,我不修改或訪問bmp
或在其他任何地方。
任何幫助,不勝感激,謝謝。
罪魁禍首是
Bitmap bmp = BitmapFactory.decodeResource(mContext.getResources(), mFileLocation);
您正在覆蓋bmp聲明,這將導致一個名為bmp的局部變量,並且不會分配給您的類變量。 您應該將其更改為
bmp = BitmapFactory.decodeResource(mContext.getResources(), mFileLocation);
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.