NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()'
Web Technologies
Web Development
.
2 years ago
0
0
0
0
+10 pts
5 Star Rating
5 Rating
_x000D_
_x000D_
public class CategoryAdapter extends BaseAdapter {
Context context;
ArrayList model;
LayoutInflater layoutInflater;
public CategoryAdapter(Activity activity, ArrayList 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?
Posted on 16 Aug 2022, this text provides information on Web Development related to Web Technologies. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Take Quiz To Earn Credits!
Turn Your Knowledge into Earnings.