开发者

Android. getHistorySize and getHistoricalX/Y

I have overridden the onTouchEvent() method in my View class, and tried to handle an EventMotion.ACTION_MOVE.

I use the following code :

if (event_.getAction() == MotionEvent.ACTION_MOVE) {  
    historySize = event_.getHistorySize();  
    endX = event_.getHistoricalX(historySize-1);  
}

I don't want to discuss the safety of this code.

On Android 2.1-update1 and Android 2.2.1 it works perfectly, but on Android 2.3.1 it crashes with an ArrayIndexOutOfBounds Exception.

What changed in getH开发者_运维知识库istorySize() and getHistoricalX() in Android 2.3.1 ?


I've just come across the same issue. It would appear that after 2.3.x getHistoricalX() and getHistoricalY() now throw an ArrayIndexOutOfBounds when passed a negative figure. Previously they just returned the first entry in the history. This would happen where historySize is 0, from a very short press,

Thus, although you do not want to discuss code safety, the answer is a check on the historySize as follows. If historySize==0, although a Move event, it hasn't actually moved so presumably you don't want to do anything.

if (event_.getAction() == MotionEvent.ACTION_MOVE) {
historySize = event_.getHistorySize();
if(historySize>0)
endX = event_.getHistoricalX(historySize-1);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜