Getting force close while scrolling down the list View in Android

女生的网名这么多〃 提交于 2020-01-11 13:04:07

问题


I am getting force close errors while I try to scroll down my list View in fragment activity, which has 1 image View and 2 text view. I am beginner in android, so don't have that much knowledge except basic scenario in mostly used components of android.

No bitmap OOM error is there, as I checked without images too.

I am retrieving images from sd card from the path.

Here, is my logcat out put with errors.

04-05 18:07:02.868: E/AndroidRuntime(3257): FATAL EXCEPTION: main
04-05 18:07:02.868: E/AndroidRuntime(3257): java.lang.NullPointerException
04-05 18:07:02.868: E/AndroidRuntime(3257):     at com.example.prac.HomeUser$ReceiptAdapter.getView(HomeUser.java:861)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.widget.AbsListView.obtainView(AbsListView.java:2588)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.widget.ListView.makeAndAddView(ListView.java:1840)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.widget.ListView.fillDown(ListView.java:681)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.widget.ListView.fillGap(ListView.java:645)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:6535)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3664)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:4492)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.View.dispatchTouchEvent(View.java:7677)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2395)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2305)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1575)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.app.Activity.dispatchTouchEvent(Activity.java:2470)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2253)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.View.dispatchPointerEvent(View.java:7875)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3978)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3862)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5105)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5084)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5182)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:174)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5155)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5201)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.Choreographer.doCallbacks(Choreographer.java:591)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.Choreographer.doFrame(Choreographer.java:559)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.os.Handler.handleCallback(Handler.java:725)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.os.Looper.loop(Looper.java:176)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at android.app.ActivityThread.main(ActivityThread.java:5302)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at java.lang.reflect.Method.invokeNative(Native Method)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at java.lang.reflect.Method.invoke(Method.java:511)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
04-05 18:07:02.868: E/AndroidRuntime(3257):     at dalvik.system.NativeStart.main(Native Method)

Now here is my code..

class ReceiptAdapter extends BaseAdapter {

    ArrayList<GetterSetter> receiptlist;
    private LayoutInflater inflater;

    // private int[] colors = new int[] { Color.parseColor("#C8A6DA"),
    // Color.parseColor("#F6F4AB"), Color.parseColor("#A2C3D0"),
    // Color.parseColor("#F1B4A1") };

    private int[] colors = new int[] { Color.parseColor("#2280aee3"),
            Color.parseColor("#22888888") };

    public ReceiptAdapter(Context context,
            ArrayList<GetterSetter> receiptlist) {
        // TODO Auto-generated method stub
        this.receiptlist = receiptlist;
        // inflater = LayoutInflater.from(context);
        // inflater = LayoutInflater.from(context.getApplicationContext());
        inflater = (LayoutInflater) getActivity().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return receiptlist.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = null;
        if (convertView == null) {
            // convertView = inflater.inflate(R.layout.custom_list, null);
            convertView = inflater.inflate(R.layout.custom_list, parent,
                    false);

            // if (position % 2 == 1) {
            // convertView.setBackgroundColor(Color.BLUE);
            // } else {
            // convertView.setBackgroundColor(Color.CYAN);
            // }

            int colorPos = position % colors.length;
            convertView.setBackgroundColor(colors[colorPos]);

            holder = new ViewHolder();
            holder.Title = (TextView) convertView.findViewById(R.id.name);
            holder.Total = (TextView) convertView.findViewById(R.id.total);
            holder.Img = (ImageView) convertView
                    .findViewById(R.id.profile_image);

            Animation animation = null;
            animation = AnimationUtils.loadAnimation(getActivity(),
                    R.anim.wave);
            animation.setDuration(200);
            convertView.startAnimation(animation);
            animation = null;

            convertView.setTag(holder);
        } else {
            convertView.getTag();
        }

        holder.Title.setText(receiptlist.get(position).getTitle());
        holder.Total.setText(receiptlist.get(position).getTotal());

        String path = receiptlist.get(position).getImg();
        File fileImg = new File(path);
        Bitmap bitmap = null;
        if (fileImg.exists()) {

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 7;



            bitmap = BitmapFactory.decodeFile(fileImg.getAbsolutePath(),
                    options);

            // bitmap = BitmapFactory.decodeFile(fileImg.getAbsolutePath());
            holder.Img.setImageBitmap(bitmap);

        }
        else
        {
            Bitmap icon = BitmapFactory.decodeResource(getActivity().getResources(),
                    R.drawable.no_image);

            holder.Img.setImageBitmap(icon);
        }

        holder.Img.setScaleType(ScaleType.CENTER_CROP);

        return convertView;
    }

    class ViewHolder {
        TextView Title;
        TextView Total;
        ImageView Img;
    }


}

and my async task in which in postExecute() I am calling the custom list adapter as,

ReceiptAdapter adapter = new ReceiptAdapter(getActivity(),
                        recList);
                setListAdapter(adapter);

So, my question is where am I getting wrong?

I have searched many codes also on stackoverflow, too but I found the same errors of others but didn't get any good feasible solution, so if anybody could help me out in this, then it would be appreciated. Thanks a ton!


回答1:


Your holder is null, always. In method getView you assign null to holder. In else you should assign tagged object to holder.

In your method, getView():

{
//...
} else {
holder = (ViewHolder)convertView.getTag();
}


来源:https://stackoverflow.com/questions/29457427/getting-force-close-while-scrolling-down-the-list-view-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!