NullPointerException:尝试调用虚拟方法'int android.graphics.Bitmap.getWidth()' [英] NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()'
问题描述
Context context;
ArrayList&ModelCategory>模型;
LayoutInflater layoutInflater;
public CategoryAdapter(Activity activity,ArrayList&ModelCategory> model){
this.model = model;
this.context = activity;
}
@Override
public int getCount(){
return model.size();
}
@Override
public Object getItem(int position){
return null;
}
@Override
public long getItemId(int position){
return position;
}
@Override
public View getView(int position,View convertView,ViewGroup parent){
ViewHolder viewHolder = null;
layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
viewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.category_adapter,parent,false);
viewHolder.imageView =(ImageView)convertView.findViewById(R.id.category_grid_image);
viewHolder.textView =(TextView)convertView.findViewById(R.id.get_category_title);
convertView.setTag(viewHolder);
} else {
viewHolder =(ViewHolder)convertView.getTag();
}
viewHolder.textView.setText(model.get(position).getCategoryName());
if(position< = 3){
viewHolder.imageView.setImageBitmap(StringToBitMap(model.get(position).getCategoryImage()));
}
else {
viewHolder.imageView.setImageBitmap(getBitmap(model.get(position).getCategoryImage()));
}
return convertView;
}
私有类ViewHolder {
public ImageView imageView;
public TextView textView;
}
私人位图getBitmap(String path){
BitmapFactory.Options选项= new BitmapFactory.Options();
option.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile(path,option);
矩阵矩阵= new Matrix();
matrix.postRotate(getImageOrientation(path));
位图rotateBitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
Bitmap resized = Bitmap.createScaledBitmap(rotateBitmap,150,150,true);
返回调整大小;
}
private static int getImageOrientation(String imagePath){
int rotate = 0;
try {
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
开关(orientation){
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch(IOException e){
e.printStackTrace();
}
return rotate;
}
public Bitmap StringToBitMap(String encodedString){
try {
byte [] encodeByte = Base64.decode(encodedString,Base64.DEFAULT) ;
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte,0,encodeByte.length);
返回位图;
} catch(Exception e){
e.getMessage();
返回null;
}
}
}
我正在获得错误
如下..
java。 lang.NullPointerException:尝试在空对象引用上调用虚拟方法'int
android.graphics.Bitmap.getWidth()'在com.adapter.CategoryAdapter.getBitmap(CategoryAdapter.java:92)
在com.adapter.CategoryAdapter.getView(CategoryAdapter.java:75)
在android.widget.AbsListView.obtainView(AbsListView.java:2896)
在android.widget.GridView.makeAndAddView( GridView.java:1456)
在android.widget.GridView.makeRow(GridView.java:361)
在android.widget.GridView.fillDown(GridView.java:302)
在android .widget.GridView.fillFromTop(GridView.java:437)
在android.widget.GridView.layoutChildren(GridView.java:1284)
在android.widget.AbsListView.onLayout(AbsLis tView.java:2700)
在android.view.View.layout(View.java:16899)
在android.view.ViewGroup.layout(ViewGroup.java:5405)
在android .widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
在android.view.View.layout(View.java:16899)
在android.view.ViewGroup.layout(ViewGroup.java:5405 )
在android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
在android.widget.FrameLayout.onLayout(FrameLayout.java:514)
在android.view.View。 layout(View.java:16899)
在android.view.ViewGroup.layout(ViewGroup.java:5405)
在android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
在android.view.View.layout(View.java:16899)
在android.view.ViewGroup.layout(ViewGroup.java:5405)
在android.widget.LinearLayout.setChildFrame (LinearLayout.java:1702)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
at android.view.View.layout (View.java:16899)
在android.view.ViewGroup.layout(ViewGroup.java:5405)
在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
在android.view.View.layout(View.java:16899)
在android.view.ViewGroup.layout(ViewGroup.java:5405)
在android.widget.FrameLayout.layoutChildren(FrameLayout.java: 579)
在android.widget.FrameLayout.onLayout(FrameLayout.java:514)
在android.view.View.layout(View.java:16899)
在android.view.ViewGroup .batry t.java:1556)
在android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
在android.view.View.layout(View.java:16899)
在android .view.ViewGroup.layout(ViewGroup.java:5405)
在android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
在android.widget.FrameLayout.onLayout(FrameLayout.java:514 )
在android.view.View.layout(View.java:16899)
在android.view.ViewGroup.layout(ViewGroup.java:5405)
在android.widget.LinearLayout。 setChildFrame(LinearLayout.java:1702)
在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
在android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
在android.view.View.layout(View.java:16899)
在android.view.ViewGroup.layout(ViewGroup.java:5405)
在android.widget.FrameLayout.layoutChildren(Fra meLayout.java:579)
在android.widget.FrameLayout.onLayout(FrameLayout.java:514)
在android.view.View.layout(View.java:16899)
在android .view.ViewGroup.layout(ViewGroup.java:5405)
我该如何解决?
错误跟踪显示错误在
位图位图= BitmapFactory.decodeFile(path,option);
位图rotateBitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
作为的文档公共静态位图decodeFile(String pathName,Options opts )
方法:
返回解码的位图,如果图像数据不能为$ b,则为null $ b解码,或者如果opts为非空值,则如果opt请求只返回
大小(在opts.outWidth和opts.outHeight中)
如果图像数据无法解码,此方法将返回null。注意日志如 E / BitmapFactory:无法解码流:...
以找出这种方法为什么失败。也许你提供了错误的路径。
public class CategoryAdapter extends BaseAdapter {
Context context;
ArrayList<ModelCategory> model;
LayoutInflater layoutInflater;
public CategoryAdapter(Activity activity, ArrayList<ModelCategory> model) {
this.model = model;
this.context = activity;
}
@Override
public int getCount() {
return model.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.category_adapter, parent, false);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.category_grid_image);
viewHolder.textView = (TextView) convertView.findViewById(R.id.get_category_title);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(model.get(position).getCategoryName());
if(position<=3){
viewHolder.imageView.setImageBitmap(StringToBitMap(model.get(position).getCategoryImage()));
}
else {
viewHolder.imageView.setImageBitmap(getBitmap(model.get(position).getCategoryImage()));
}
return convertView;
}
private class ViewHolder {
public ImageView imageView;
public TextView textView;
}
private Bitmap getBitmap(String path) {
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile(path, option);
Matrix matrix = new Matrix();
matrix.postRotate(getImageOrientation(path));
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
Bitmap resized = Bitmap.createScaledBitmap(rotatedBitmap, 150, 150, true);
return resized;
}
private static int getImageOrientation(String imagePath) {
int rotate = 0;
try {
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return rotate;
}
public Bitmap StringToBitMap(String encodedString){
try {
byte [] encodeByte=Base64.decode(encodedString, Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
} catch(Exception e) {
e.getMessage();
return null;
}
}
}
I am getting error
like below..
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference at com.adapter.CategoryAdapter.getBitmap(CategoryAdapter.java:92) at com.adapter.CategoryAdapter.getView(CategoryAdapter.java:75) at android.widget.AbsListView.obtainView(AbsListView.java:2896) at android.widget.GridView.makeAndAddView(GridView.java:1456) at android.widget.GridView.makeRow(GridView.java:361) at android.widget.GridView.fillDown(GridView.java:302) at android.widget.GridView.fillFromTop(GridView.java:437) at android.widget.GridView.layoutChildren(GridView.java:1284) at android.widget.AbsListView.onLayout(AbsListView.java:2700) at android.view.View.layout(View.java:16899) at android.view.ViewGroup.layout(ViewGroup.java:5405) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077) at android.view.View.layout(View.java:16899) at android.view.ViewGroup.layout(ViewGroup.java:5405) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) at android.widget.FrameLayout.onLayout(FrameLayout.java:514) at android.view.View.layout(View.java:16899) at android.view.ViewGroup.layout(ViewGroup.java:5405) at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043) at android.view.View.layout(View.java:16899) at android.view.ViewGroup.layout(ViewGroup.java:5405) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556) at android.widget.LinearLayout.onLayout(LinearLayout.java:1465) at android.view.View.layout(View.java:16899) at android.view.ViewGroup.layout(ViewGroup.java:5405) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077) at android.view.View.layout(View.java:16899) at android.view.ViewGroup.layout(ViewGroup.java:5405) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) at android.widget.FrameLayout.onLayout(FrameLayout.java:514) at android.view.View.layout(View.java:16899) at android.view.ViewGroup.layout(ViewGroup.java:5405) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556) at android.widget.LinearLayout.onLayout(LinearLayout.java:1465) at android.view.View.layout(View.java:16899) at android.view.ViewGroup.layout(ViewGroup.java:5405) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) at android.widget.FrameLayout.onLayout(FrameLayout.java:514) at android.view.View.layout(View.java:16899) at android.view.ViewGroup.layout(ViewGroup.java:5405) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556) at android.widget.LinearLayout.onLayout(LinearLayout.java:1465) at android.view.View.layout(View.java:16899) at android.view.ViewGroup.layout(ViewGroup.java:5405) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) at android.widget.FrameLayout.onLayout(FrameLayout.java:514) at android.view.View.layout(View.java:16899) at android.view.ViewGroup.layout(ViewGroup.java:5405)
How can I solve this?
Error traces show that error occours on
Bitmap bitmap = BitmapFactory.decodeFile(path, option);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
As the documentation of the public static Bitmap decodeFile(String pathName, Options opts)
method:
return The decoded bitmap, or null if the image data could not be
decoded, or, if opts is non-null, if opts requested only the
size be returned (in opts.outWidth and opts.outHeight)
This method will return null, if the image data could not be decoded. Note logs like E/BitmapFactory: Unable to decode stream: ...
to find out this method why failed. Maybe you provided a wrong path.
这篇关于NullPointerException:尝试调用虚拟方法'int android.graphics.Bitmap.getWidth()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!