滚动时位图上的空指针异常

时间:2014-11-02 18:45:47

标签: android facebook bitmap nullpointerexception

我正在创建一个位图,将Facebook图像裁剪成圆形,代码工作正常但是在同一个活动中有很多图像,它给出了空指针异常,而滚动这里是我的代码

//enter code here
byte[] decodedString = Base64.decode(obj.picture, Base64.DEFAULT);
                            Bitmap decodedByte =  BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                     on this line i am getting a null pointer exception
                            Bitmap circleBitmap = Bitmap.createBitmap(decodedByte.getWidth(), decodedByte.getHeight(), Bitmap.Config.ARGB_8888);
                            BitmapShader shader = new BitmapShader(decodedByte, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
                            Paint paint = new Paint();
                            paint.setShader(shader);
                            Canvas c = new Canvas(circleBitmap);
                            c.drawCircle(decodedByte.getWidth() / 2, decodedByte.getHeight() / 2, decodedByte.getWidth() / 2, paint);
                            holder.civilianImage.setImageBitmap(circleBitmap);

我可以看到显示的图像,但在滚动时会显示此错误。 任何帮助,将不胜感激。 还有一件事是我从Facebook获得图像,但每张图片在分辨率上都与另一张图像不同,而裁剪图像却失去了很多,我怎么能管理它。

enter code here
public class ListViewAdapter extends ArrayAdapter<NewsFeed>
{
    public static final int missionLayout = 0;
    public static final int badgeLayout = 1;
    private static final int TYPE_MAX_COUNT = badgeLayout + 1;
    private NewsFeed obj;
    private LayoutInflater mInflater;
    public ListViewAdapter ()
    {
        super(CityNewsFeedActivity.this,R.layout.activity_activity_city_news_feed,newsfeed);
        mInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getViewTypeCount()

    {
        return TYPE_MAX_COUNT;
    }

    @Override
    public int getItemViewType(int position)
    {
         obj = newsfeed.get(position);
        return obj.category.equals("mission")? missionLayout : badgeLayout;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder=null;

        if (convertView == null)
        {

            holder = new ViewHolder();
   if (obj.accepted_rejected.equals("success"))
                        {
                            holder.relativeLayoutMission.setBackgroundResource(R.drawable.mission_successful);
                            Float alpha = Float.valueOf("0.9");
                            holder.relativeLayoutMission.setAlpha(alpha);
                            byte[] decodedString = Base64.decode(obj.picture, Base64.DEFAULT);
                            Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                            Bitmap circleBitmap = Bitmap.createBitmap(decodedByte.getWidth(), decodedByte.getHeight(), Bitmap.Config.ARGB_8888);
                            BitmapShader shader = new BitmapShader(decodedByte, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
                            Paint paint = new Paint();
                            paint.setShader(shader);
                            Canvas c = new Canvas(circleBitmap);
                            c.drawCircle(decodedByte.getWidth() / 2, decodedByte.getHeight() / 2, decodedByte.getWidth() / 2, paint);
                            holder.civilianImage.setImageBitmap(circleBitmap);
                          }
                      }
                return convertView;
                  }
                 }
enter code here
11-02 20:36:11.344  30864-30864/net.httpiamheroic.herioc E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: net.httpiamheroic.herioc, PID: 30864
java.lang.NullPointerException
        at net.httpiamheroic.herioc.net.httpiamherioc.Activities.CityNewsFeedActivity$ListViewAdapter.getView(CityNewsFeedActivity.java:419)
        at android.widget.AbsListView.obtainView(AbsListView.java:2450)
        at android.widget.ListView.makeAndAddView(ListView.java:1891)
        at android.widget.ListView.fillDown(ListView.java:792)
        at android.widget.ListView.fillGap(ListView.java:756)
        at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5728)
        at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4826)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:807)
        at android.view.Choreographer.doCallbacks(Choreographer.java:601)
        at android.view.Choreographer.doFrame(Choreographer.java:561)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:791)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5867)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
        at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

像这样更改你的getView方法 还要检查obj.accepted_rejected.equals(“success”)是否为false

 @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder=null;
        // Your row view 
        View rowView = convertView;

        if (rowView == null)
        {
            // New holder object 
            holder = new ViewHolder();
            holder.civilianImage = rowView.findViewById(R.id.civilianimageview);// Your xml image view id

                            rowView.setTag(holder);

           } else{
                // Use existing holder object 
              holder=(ViewHolder) rowView.getTag();
           }
        if (obj.accepted_rejected.equals("success")){
            holder.relativeLayoutMission.setBackgroundResource(R.drawable.mission_successful);
            Float alpha = Float.valueOf("0.9");
            holder.relativeLayoutMission.setAlpha(alpha);
            byte[] decodedString = Base64.decode(obj.picture, Base64.DEFAULT);
            Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
            Bitmap circleBitmap = Bitmap.createBitmap(decodedByte.getWidth(), decodedByte.getHeight(), Bitmap.Config.ARGB_8888);
            BitmapShader shader = new BitmapShader(decodedByte, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            Paint paint = new Paint();
            paint.setShader(shader);
            Canvas c = new Canvas(circleBitmap);
            c.drawCircle(decodedByte.getWidth() / 2, decodedByte.getHeight() / 2, decodedByte.getWidth() / 2, paint);
            holder.civilianImage.setImageBitmap(circleBitmap);
        }else{
            // Handle this case
        }
          return rowView;
     }

答案 1 :(得分:0)

不幸的是(愚蠢地)与Android中的图像处理有关的多个方法在提供的数据不是图像时不会抛出异常,它们只返回null ... WTH?

因为看起来错误的原因是由于错误输入:

byte[] decodedByte = BitmapFactory.decodeByteArray(...)

返回 null ,并强制应用在尝试使用 decodingByte 的下一行崩溃

Bitmap circleBitmap = Bitmap.createBitmap(decodedByte.getWidth(), decodedByte.getHeight(), Bitmap.Config.ARGB_8888);