【问题标题】:Android :NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object referenceAndroid:NullPointerException:尝试在空对象引用上调用虚拟方法“int android.graphics.Bitmap.getWidth()”
【发布时间】:2015-08-28 09:04:51
【问题描述】:

我正在尝试将图像从 sd 卡获取到位图中以在图像视图中显示。运行应用程序后,会显示前两到三个图像,但是当我滚动列表应用程序崩溃并出现 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)

【问题讨论】:

    标签: android


    【解决方案1】:

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

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

    【讨论】:

    • 但是在我的String objElement中:已经得到了像objElement这样的SD卡路径:= /mnt/sdcard/SDImages/95_20150824112324025.png。
    • 我已经更新了答案,也参考stackoverflow.com/a/8710690/1552136。如果不能解决这个问题,请告诉我我会试试这个。
    • 仍然得到 Bitmap is null ,代码有什么问题。
    • 你在你的设备或模拟器上在哪里测试这个?
    • 在模拟器和测试设备上进行测试。
    【解决方案2】:

    尝试用

    替换 Bitmap bitmapSize = BitmapFactory.decodeFile(objElement);
      File file = new File(objElement);
      if (file.exists()) {
         Bitmap bitmapSize = BitmapFactory.decodeFile(file.getAbsolutePath());
      }
    

    【讨论】:

    • 你为什么使用new File(objElement).getAbsolutePath()?为什么不file.getAbsolutePath()
    【解决方案3】:

    您似乎有无法解码的图像。

    来自文档:

    /**
     * Decode a file path into a bitmap. If the specified file name is null,
     * or cannot be decoded into a bitmap, the function returns null.
     *
     * @param pathName complete path name for the file to be decoded.
     * @return the resulting decoded bitmap, or null if it could not be decoded.
     */
    public static Bitmap decodeFile(String pathName) {
        return decodeFile(pathName, null);
    }
    

    【讨论】:

    • 所以当你在 bitmap.getCongif() 上得到一个空指针异常时,这个 sn-p 应该有助于解码它?
    • @user3919920 不,这无济于事。这个 sn-p 显示了 Android 源代码的片段,它解释了为什么可以返回 null。
    【解决方案4】:

    我知道我这个答案有点晚了。但是今天我面临同样的问题。在毕加索中使用位图设置图像并没有解决我的问题。我尝试了下面的一个。

    Picasso.with(holder.mRootView.getContext()).load(new File(trackingNumbers.get(position).getImageUrl())).error(R.drawable.no_image_place_holder).noFade().placeholder(R.drawable.no_image_place_holder).into(circularImageView);
    

    谢谢

    【讨论】:

      猜你喜欢
      • 2017-03-19
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2020-11-28
      • 1970-01-01
      • 2015-01-24
      • 2020-09-17
      相关资源
      最近更新 更多