为什么我的应用程序崩溃在Android中的代码中设置Listview的背景颜色?

时间:2014-04-17 07:52:30

标签: android listview background-color

我在ListView中选择一行时设置了一种颜色。我收到以下错误日志。请告诉我我做错了什么。我的代码如下:

    View view = inflater.inflate(R.layout.friendsretrieval, container,
            false);
    final SwipeListView swipy=(SwipeListView)view.findViewById(R.id.example_swipe_lv_list);
    swipy.setSwipeMode(SwipeListView.SWIPE_MODE_RIGHT);

   ListAdapter k=new SimpleAdapter(getActivity(),val,R.layout.custom_row,new String[]{"fbname"},new int[]{R.id.example_itemname}){

        @Override
        public View getView(int position, final View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
             final View v = super.getView(position, convertView, parent);
             final ImageView ii=(ImageView)v.findViewById(R.id.example_imagess);

             swipy.setOffsetRight(convertDpToPixel(280f));

        @Override
                public void onMove(int position, float x) {
                    // int i=0;
                    Log.i("moved position",""+x);
                    //Toast.makeText(getActivity(),"moved"+position+""+x,Toast.LENGTH_SHORT).show();
                    if(x>0)
                    {
                        ii.setVisibility(v.VISIBLE);
                        convertView.setBackgroundColor(Color.BLUE);
                    }
                    else 
                    {
                        ii.setVisibility(v.INVISIBLE);
                    }
                }

    });

            return v;
        }

    };

    swipy.setAdapter(k);


    return view;
}

错误日志:

 04-17 03:47:07.540: E/InputEventReceiver(7473): Exception dispatching input event.
 -17 03:47:07.690: E/AndroidRuntime(7473): FATAL EXCEPTION: main
  -17 03:47:07.690: E/AndroidRuntime(7473): Process: com.example.taskmanager, PID: 7473
 04-17 03:47:07.690: E/AndroidRuntime(7473): java.lang.NullPointerException
 -17 03:47:07.690: E/AndroidRuntime(7473):  at com.example.taskmanager.myfriends$1$1.onMove(myfriends.java:132)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at com.fortysevendeg.swipelistview.SwipeListView.onMove(SwipeListView.java:491)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at com.fortysevendeg.swipelistview.SwipeListViewTouchListener.move(SwipeListViewTouchListener.java:902)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at com.fortysevendeg.swipelistview.SwipeListViewTouchListener.onTouch(SwipeListViewTouchListener.java:875)
 -17 03:47:07.690: E/AndroidRuntime(7473):  at android.view.View.dispatchTouchEvent(View.java:7701)
 -17 03:47:07.690: E/AndroidRuntime(7473):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
 -17 03:47:07.690: E/AndroidRuntime(7473):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
 04-17 03:47:07.690: E/AndroidRuntime(7473):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
 -17 03:47:07.690: E/AndroidRuntime(7473):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
 -17 03:47:07.690: E/AndroidRuntime(7473):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
  -17 03:47:07.690: E/AndroidRuntime(7473):     at  android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
  -17 03:47:07.690: E/AndroidRuntime(7473):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
  -17 03:47:07.690: E/AndroidRuntime(7473):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
  -17 03:47:07.690: E/AndroidRuntime(7473):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
   04-17 03:47:07.690: E/AndroidRuntime(7473):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
   04-17 03:47:07.690: E/AndroidRuntime(7473):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
   04-17 03:47:07.690: E/AndroidRuntime(7473):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
   04-17 03:47:07.690: E/AndroidRuntime(7473):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
   -17 03:47:07.690: E/AndroidRuntime(7473):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
   04-17 03:47:07.690: E/AndroidRuntime(7473):  at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
   04-17 03:47:07.690: E/AndroidRuntime(7473):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.View.dispatchPointerEvent(View.java:7886)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
   04-17 03:47:07.690: E/AndroidRuntime(7473):  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
   04-17 03:47:07.690: E/AndroidRuntime(7473):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
   04-17 03:47:07.690: E/AndroidRuntime(7473):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
  -17 03:47:07.690: E/AndroidRuntime(7473):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
    04-17 03:47:07.690: E/AndroidRuntime(7473):     at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
 04-17 03:47:07.690: E/AndroidRuntime(7473):    at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5655)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5701)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.Choreographer.doCallbacks(Choreographer.java:574)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.Choreographer.doFrame(Choreographer.java:542)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
 -17 03:47:07.690: E/AndroidRuntime(7473):  at android.os.Handler.handleCallback(Handler.java:733)
  04-17 03:47:07.690: E/AndroidRuntime(7473):   at android.os.Handl

1 个答案:

答案 0 :(得分:1)

convertViewnull

如果convertView为空,您将需要对行XML进行充气并使用它 - 只有在有要回收的内容时才会填充它。

这样的事情:

 @Override
 public View getView(int position, final View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null)
        v = super.getView(position, convertView, parent);
    final ImageView ii=(ImageView)v.findViewById(R.id.example_imagess);

    swipy.setOffsetRight(convertDpToPixel(280f));

    @Override
    public void onMove(int position, float x) {
        if(x>0) {
            ii.setVisibility(v.VISIBLE);
            v.setBackgroundColor(Color.BLUE);
        }
        else  {
            ii.setVisibility(v.INVISIBLE);
        }
    }
    });
   return v;
}

Documentation

  

如果可能的话,重用旧视图。注意:在使用之前,应检查此视图是否为非null且类型是否合适。如果无法转换此视图以显示正确的数据,则此方法可以创建新视图