Java 位图大小调整
我的代码中出现NullPointerException错误:Java 位图大小调整,java,android,bitmap,Java,Android,Bitmap,我的代码中出现NullPointerException错误: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.graphics.Bitmap.getWidth() 我有一个位图,我从web服务接收的字节中解码了它。我不理解这个错误。你能帮我更正代码吗 我是Android编程新手,我渴望学习 这是我的密码: public class CatListAdapter extends ArrayAdapter { Array
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.graphics.Bitmap.getWidth()
我有一个位图,我从web服务接收的字节中解码了它。我不理解这个错误。你能帮我更正代码吗
我是Android编程新手,我渴望学习
这是我的密码:
public class CatListAdapter extends ArrayAdapter {
ArrayList<String> alNames;
ArrayList<String> alImages;
ArrayList<String> alId;
private Activity context;
float cornerRadius = 50.0f;
ImageView imageFlag;
public CatListAdapter(Activity context, ArrayList<String> alNames, ArrayList<String> alImages,
ArrayList<String> alId) {
super(context, R.layout.category_list, alNames);
this.context = context;
this.alNames = alNames;
this.alImages = alImages;
this.alId = alId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null)
row = inflater.inflate(R.layout.category_list, null, true);
TextView textViewCountry = (TextView) row.findViewById(R.id.txtcatname);
imageFlag = (ImageView) row.findViewById(R.id.catimage);
textViewCountry.setText(alNames.get(position));
if (String.valueOf(alImages.get(position)) != "") {
byte[] decode = Base64.decode(alImages.get(position), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
bitmapResize(bitmap);
}
return row;
}
public void bitmapResize(Bitmap imageBitmap) {
float widthbmp = imageBitmap.getWidth();
float lengthbmp = imageBitmap.getHeight();
// Get Screen width
DisplayMetrics displaymetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
float hight = displaymetrics.heightPixels / 3;
float width = displaymetrics.widthPixels / 3;
int convertHighet = (int) hight, convertWidth = (int) width;
// high length
if (lengthbmp > hight) {
convertHighet = (int) hight - 20;
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, convertWidth, convertHighet, true);
}
// high width
if (widthbmp > width) {
convertWidth = (int) width - 20;
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, convertWidth, convertHighet, true);
imageFlag.setImageBitmap(imageBitmap);
}
}
}
公共类CatListAdapter扩展了ArrayAdapter{
ArrayList-alNames;
ArrayList alImages;
ArrayList alId;
私人活动语境;
浮动转角半径=50.0f;
ImageView imageFlag;
公共CatListAdapter(活动上下文、ArrayList alNames、ArrayList alImages、,
ArrayList(alId){
超级(上下文、R.layout.category_列表、ALNAME);
this.context=上下文;
this.alNames=alNames;
这个.alImages=alImages;
this.alId=alId;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
LayoutInflater充气器=上下文。getLayoutInflater();
if(convertView==null)
行=充气机。充气(R.layout.category_列表,空,真);
TextView textViewCountry=(TextView)row.findViewById(R.id.txtcatname);
imageFlag=(ImageView)row.findViewById(R.id.catimage);
textViewCountry.setText(alNames.get(position));
if(String.valueOf(alImages.get(position))!=“”){
字节[]decode=Base64.decode(alImages.get(位置),Base64.DEFAULT);
位图位图=位图工厂.decodeByteArray(decode,0,decode.length);
位图调整大小(位图);
}
返回行;
}
公共无效位图调整大小(位图图像位图){
float widthbmp=imageBitmap.getWidth();
float length bmp=imageBitmap.getHeight();
//获取屏幕宽度
DisplayMetrics DisplayMetrics=新的DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
浮点高=displaymetrics.heightPixels/3;
浮动宽度=displaymetrics.widthPixels/3;
int convertHighet=(int)high,convertWidth=(int)width;
//高长度
如果(长度BMP>高度){
convertHighet=(int)hight-20;
imageBitmap=Bitmap.createScaledBitmap(imageBitmap,convertWidth,convertHighet,true);
}
//高宽度
如果(宽度BMP>宽度){
convertWidth=(int)宽度-20;
imageBitmap=Bitmap.createScaledBitmap(imageBitmap,convertWidth,convertHighet,true);
设置图像位图(图像位图);
}
}
}
首先请检查字节[]解码
是否返回非空值。
然后在前面添加以下代码行:
Bitmap Bitmap=BitmapFactory.decodeByteArray(decode,0,decode.length)代码>
BitmapFactory.Options bmOptions=new-BitmapFactory.Options();
bmOptions.inJustDecodeBounds=false代码>
您可以使用以下选项:
Bitmap Bitmap=BitmapFactory.decodeByteArray(decode,0,decode.length,bmOptions)代码>
要了解实施,您可以参考以下链接:
快乐编码 我猜在代码Bitmap Bitmap=BitmapFactory.decodeByteArray(decode,0,decode.length)中,您得到了一个空位图,所以您可以发布alImages.get(position)的a值。您的位图对象为空以避免它,只需使用if语句检查是否为空。如果您有url,请使用任何图像加载库,如glide或picasso.thanq u frds I willif(String.valueOf(alImages.get(position))!=“”)这里我正在检查是否为空。先生,我得到了输出