向右下方滚动gridview时出现空指针异常

时间:2015-07-07 19:15:12

标签: android xml gridview

我正在制作一个Android应用程序,我在其中制作了一个自定义库。当我在网格中显示图像时它工作正常,但当我滚动gridview下来获取空指针异常 我正在使用Holderview类,而不是在使用Activity。我正在研究碎片

这是我的getview功能

   public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            View rootview=convertView;
            if (convertView == null) {
                holder = new ViewHolder();
                rootview = mInflater.inflate(
                        R.layout.gallery_item, null);
                holder.imageview = (ImageView) rootview.findViewById(R.id.thumbImage);

                rootview.setTag(holder);
            }
            else {
                holder = (ViewHolder) rootview.getTag();
            }
            holder.imageview.setId(position);

            holder.imageview.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int id = v.getId();
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
                    startActivity(intent);
                }
            });

            holder.imageview.setImageBitmap(thumbnails[position]);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            thumbnails[position].compress(Bitmap.CompressFormat.JPEG, 100, baos);        //here is the error Null Pointer Exception
            byte[] b = baos.toByteArray();

            String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

            holder.id = position;
            return rootview;
        }






    }
    static class ViewHolder {
        ImageView imageview;
        static int id;
    }

这是我的LOGCAT

 java.lang.NullPointerException
            at com.tabdemo.Tab1Fragment$ImageAdapter.getView(Tab1Fragment.java:117)
            at android.widget.AbsListView.obtainView(AbsListView.java:2186)
            at android.widget.GridView.makeAndAddView(GridView.java:1341)
            at android.widget.GridView.makeRow(GridView.java:341)
            at android.widget.GridView.fillDown(GridView.java:283)
            at android.widget.GridView.fillGap(GridView.java:243)
            at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5257)
            at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3179)
            at android.widget.AbsListView.onTouchEvent(AbsListView.java:3506)
            at android.view.View.dispatchTouchEvent(View.java:7263)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1932)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1977)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1427)
            at android.app.Activity.dispatchTouchEvent(Activity.java:2428)
            at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1925)
            at android.view.View.dispatchPointerEvent(View.java:7443)
            at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3603)
            at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3531)
            at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4783)
            at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4743)
            at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4895)
            at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
            at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
            at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171)
            at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4863)
            at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4917)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
            at android.view.Choreographer.doCallbacks(Choreographer.java:579)
            at android.view.Choreographer.doFrame(Choreographer.java:546)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5371)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

确保阵列中有足够的缩略图。如果它在这里崩溃:

thumbnails[position].compress(Bitmap.CompressFormat.JPEG, 100, baos);        //here is the error Null Pointer Exception

这意味着thumbnails[position]null。确保您的数组不为空,并且每个项目位置都有值。

答案 1 :(得分:-1)

检查thumbnails的值,然后从thumbnails获取值是否为空

if(thumbnails[position]!=null)
{
   holder.imageview.setImageBitmap(thumbnails[position]);
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   thumbnails[position].compress(Bitmap.CompressFormat.JPEG, 100,baos);      
   byte[] b = baos.toByteArray();
   String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
}