2012-06-29 26 views
3

在我的Android应用程序之一,我使用自定义的画廊,以显示画廊图像。 (我交换画廊时使用,以显示1项的时间定制图库)空指针异常:GestureDetector.onTouchEvent在Android的定制库的ICS 4.0

这里是我使用自定义库中的代码:

public class CustomGallery extends Gallery { 

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

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

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

    private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) { 
     return e2.getX() > e1.getX(); 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
      float velocityY) { 
     int kEvent; 
     if (isScrollingLeft(e1, e2)) { // Check if scrolling left 
      kEvent = KeyEvent.KEYCODE_DPAD_LEFT; 
     } else { // Otherwise scrolling right 
      kEvent = KeyEvent.KEYCODE_DPAD_RIGHT; 
     } 
     onKeyDown(kEvent, null); 
     return true; 

    } 
} 

上面的代码工作正常2.2 ,2.3等....但它的在ICS 4.0中崩溃导致空指针异常GestureDetector.onTouchEvent

请帮助。

在此先感谢。

+2

我只注意到这个问题是因为....定制的画廊或所有这些不是.....问题是由于接头主机....当我尝试使用StartActvity()方法启动actvity时,它不会崩溃,但是当我开始将activity作为当前活动的子节点时......在标签主机中..崩溃来临......任何一个有这么一个想法,因为我必须开始这个孩子,而不是一个单一的行为....这里是我的代码:** Intent i = new Intent(A.this,A_Detail.class); \t \t \t \t TabGroupActivity parent =(TabGroupActivity)getParent(); \t \t \t (父).startChildActivity(PLAT_PAGE_ID,I); ** –

回答

0

我有同样的问题只发生在ICS4.0 - 我GalleryView是打开TabHost内的Activity当用户点击Gallery一个项目 - 它总是给人NullPointerException但仅限于ICS4 - 我最后做的接下来的诀窍:

//flag returned by onTouch event always false except when we are about to start activity 
boolean flag = false; 
//add a touch listener 
myGallery.setOnTouchListener(new OnTouchListener() { 
@Override 
    public boolean onTouch(View v, MotionEvent event) { 
     return flag; 
    } 
}); 

myGallery.setOnItemClickListener(new OnItemClickListener() { 
    //handle clicks 

    //set flag returned by touch listener to true 
    flag = true; 

    //now add logic to open up the activity 
} 

例外现在已经在ICS4上。

0

我有同样的问题零星。传递给覆盖onFling方法的两个MotionEvent参数有时为空,并调用e2.getX()引发异常。您可以通过启动您的onFling方法是这样解决这个问题:

if (e1 == null || e2 == null) return false;