LoginSignup
3
3

More than 5 years have passed since last update.

ListViewを継承してonTouchEventを実装するとNullPointerException(Android 2.3以前)になるので回避策でgetChildCount使う実装

Posted at
HogeListView.java
public class HogeListView extends ListView {

    public HogeListView(Context context) {
        super(context);
    }

    public HogeListView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
    }  

    public HogeListView(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);  
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
        case MotionEvent.ACTION_MOVE:
            // task
            break;
        default:
            break;
        }

        if (getChildCount() == 0) {
            return false;
        }

        return super.onTouchEvent(ev);
    }
}

参考
http://stackoverflow.com/questions/12473625/nullpointerexception-at-android-widget-abslistview-contentfitsabslistview-java

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3