Helppp Meeee
我拥有数据库并检索blob并将其转换为具有其他数据的图像, 但是我尝试将字节转换为位图并错误如何初始化byte.length 我认为这是错误的操作方法,我会采取什么策略
这是我的日志,并指向空对象
2019-10-16 09:02:27.009 24408-24408/com.ilsproj.ilovebatangas E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ilsproj.ilovebatangas, PID: 24408
java.lang.NullPointerException: Attempt to invoke interface method 'long java.sql.Blob.length()' on a null object reference
at com.ilsproj.ilovebatangas.adapter.Recyclerview_Mountain.onBindViewHolder(Recyclerview_Mountain.java:56)
at com.ilsproj.ilovebatangas.adapter.Recyclerview_Mountain.onBindViewHolder(Recyclerview_Mountain.java:26)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at androidx.recyclerview.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:557)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at androidx.recyclerview.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
at android.view.View.layout(View.java:19681)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19681)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19681)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
at android.view.View.layout(View.java:19681)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19681)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19681)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:772)
at android.view.View.layout(View.java:19681)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2507)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2223)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1403)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6804)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
2019-10-16 09:02:27.010 24408-24408/com.ilsproj.ilovebatangas E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:169)
at android.app.ActivityThread.main(ActivityThread.java:6521)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
这是我的适配器
public class Recyclerview_Mountain extends RecyclerView.Adapter<Recyclerview_Mountain.MyViewHolder>
{
private Context mContext;
private List<Mountain> lstcat;
int blobLength = 0;
public Recyclerview_Mountain(Context mContext1, List<Mountain> lstcat) {
this.mContext = mContext1;
this.lstcat = lstcat;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.cardview_item_data, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
holder.mountainName.setText(lstcat.get(position).getMountain_name());
Blob blob =lstcat.get(position).getMimage();
try {
blobLength = (int) blob.length();
byte[] blobAsBytes;
blobAsBytes = blob.getBytes(1, blobLength);
ByteArrayInputStream imageStream = new ByteArrayInputStream(blobAsBytes);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
holder.categoryImage.setImageBitmap(theImage);
} catch (SQLException e) {
e.printStackTrace();
}
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(mContext, Cat_Mountains_Details.class);
// Data ito nang category
intent.putExtra("Name",lstcat.get(position).getMountain_name());
intent.putExtra("Latitude",lstcat.get(position).getLatitude());
intent.putExtra("Longitude",lstcat.get(position).getLongitude());
intent.putExtra("Description",lstcat.get(position).getMdescription());
intent.putExtra("Type",lstcat.get(position).getMtype());
intent.putExtra("Longitude",lstcat.get(position).getLongitude());
// intent.putExtra("Thumbnail", lstcat.get(position).getThumbnail());
// ito ay mag start nang activity
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return lstcat.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView mountainName;
ImageView categoryImage;
CardView cardView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
mountainName = itemView.findViewById(R.id.category_title);
categoryImage = itemView.findViewById(R.id.category_image);
cardView = itemView.findViewById(R.id.cardview_id);
}
}
}