滚动列表时关闭力量在Android中查看

时间:2015-04-05 12:46:52

标签: android listview scrollview baseadapter android-logcat

当我尝试向下滚动列表视图片段活动时,我收到强制关闭错误,其中包含1个图像视图和2个文本视图。我是android的初学者,所以除了在android中使用最多的组件的基本场景之外,没有那么多的知识。

没有位图OOM错误,因为我也检查了没有图像。

我正从路径中的SD卡中检索图像。

这里,我的logcat输出有错误。

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)

现在这是我的代码..

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;
    }


}

和我的异步任务,在postExecute()中我将自定义列表适配器称为,

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

所以,我的问题是我哪里出错?

我也在stackoverflow上搜索了很多代码,但我发现其他人的错误相同,但没有得到任何好的可行解决方案,所以如果有人可以帮我解决这个问题,那么我们将不胜感激。万分感谢!

1 个答案:

答案 0 :(得分:1)

你的持有人总是null。在方法getView中,您为持有者分配null。 在其他情况下,您应该将标记对象分配给持有者。

在你的方法中,getView():

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