• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Kotlin - 'break' and 'continue' are only allowed inside a loop

umtblbl

Member
  • Marked line with stars codes fail in a case in the When loop .

  • Error Description : 'break' and 'continue' are only allowed inside a loop Please help me, thank How do I implement a solution?
Code:
if (mOpenedItems.size == 0) break //**************************************ERROR**************
// Disallow our parent Views to intercept the touch events so long as there is

// at least one item view in the open or being closed state.
requestParentDisallowInterceptTouchEvent()
if (mFullyOpenedItem != null) {
    mHasItemFullyOpenOnActionDown = true
    if (mActiveItem === mFullyOpenedItem) {
        resolveActiveItemMenuBounds()
        // If the user's finger downs on the completely opened itemView's menu area,
        // do not intercept the subsequent touch events (ACTION_MOVE, ACTION_UP, etc.)
        // as we receive the ACTION_DOWN event.
        // If the user's finger downs on the fully opened itemView but not on
        // its menu, then we need to intercept them.
        if (mActiveItemMenuBounds.contains(mDownX, mDownY)) {
            break //*******************************ERROR*****************************************
        } else if (mActiveItemBounds.contains(mDownX, mDownY)) {
            return true
        }
    }
    // If 1) the fully opened itemView is not the current one or 2) the user's
    // finger downs outside of the area in which this view displays the itemViews,
    // make the itemView's menu hidden and intercept the subsequent touch events.
    releaseItemViewInternal(mFullyOpenedItem, itemScrollDuration)
}
 
Last edited:
I've never looked at a line of Kotlin before this morning, though apart from the lack of semi-colons this looks a lot like C++. But I think that if you want people to help it would be useful to actually see the loop that these statements are supposed to be inside. If there's an error before this that means the compiler/interpreter thinks that loop has already ended we cannot spot that from the snippet here. And since one of the errors is on the first line of this snippet it's highly likely that the explanation is in one of the preceding lines which you've not included.
 
problem is solved.
Code:
when (e.action) {
    MotionEvent.ACTION_DOWN -> run {

and

Code:
if (mOpenedItems.size == 0) return@run

// Disallow our parent Views to intercept the touch events so long as there is

// at least one item view in the open or being closed state.
requestParentDisallowInterceptTouchEvent()
if (mFullyOpenedItem != null) {
    mHasItemFullyOpenOnActionDown = true
    if (mActiveItem === mFullyOpenedItem) {
        resolveActiveItemMenuBounds()
        // If the user's finger downs on the completely opened itemView's menu area,
        // do not intercept the subsequent touch events (ACTION_MOVE, ACTION_UP, etc.)
        // as we receive the ACTION_DOWN event.
        // If the user's finger downs on the fully opened itemView but not on
        // its menu, then we need to intercept them.
        if (mActiveItemMenuBounds.contains(mDownX, mDownY)) {
            return@run
        } else if (mActiveItemBounds.contains(mDownX, mDownY)) {
            return true
        }
    }
 
Back
Top Bottom