gpt4 book ai didi

安卓 :NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:57:59 32 4
gpt4 key购买 nike

我正在尝试从 SD 卡获取图像到位图中以在 ImageView 中显示。运行应用程序后,首先显示两到三个图像,但是当我滚动列表时应用程序崩溃并出现 NullPointerException 异常:java.lang .NullPointerException:尝试在空对象引用上调用虚拟方法“int android.graphics.Bitmap.getWidth()” 在 com.example.tazeen.classnkk.Activity1$MyListAdapter.getView(Activity1.java:357)

                if(objElement.endsWith(mp3_Pattern))
{
Log.e("Mp3 ", " ends with ");

{
int scalFactor = fixHeight / h ;
newWidth = (int)( w * scalFactor);
Log.e("scalFactor "," = " + scalFactor);
Log.e("newWidth "," = " + newWidth);
Log.e("resizing... ","in if condition");
new_Height = fixHeight ;
}
else
{
newWidth = w;
new_Height = h;
}


Uri uri = Uri.fromFile(new File(objElement));
Picasso.with(getContext()).load(uri).resize(newWidth, new_Height).placeholder(R.drawable.img_placeholder).into(imageView , new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
if (pBar != null) {
pBar.setVisibility(View.GONE);
}
}
@Override
public void onError() {

}
});
}

holder.linearLayout.addView(imageView);
holder.linearLayout.addView(pBar);

这里是日志信息

08-28 14:22:14.077    2947-2947/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.tazeen.classnkk, PID: 2947
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
at com.example.tazeen.classnkk.AllPosts_Page$MyListAdapter.getView(AllPosts_Page.java:357)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillGap(ListView.java:662)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4991)
at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3418)
at android.widget.AbsListView.onTouchMove(AbsListView.java:3801)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3632)
at android.view.View.dispatchTouchEvent(View.java:8471)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2399)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2092)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)

最佳答案

试试这个。在你的里面如果(objElement.endsWith(png_Pattern))

  BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(objElement, options);

关于安卓 :NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32267265/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com