3

I'am trying to show a LazyColumn inside an alert dialog so the user can choose between a list of items, and click on it. The alert dialog will show without problem, i can click on any item which is on screen and close it, but as soon as i try to scroll between the items, it will give the following error:

E/InputEventReceiver: Exception dispatching input event.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.app, PID: 22418
    java.lang.IllegalArgumentException: Failed requirement.
        at androidx.compose.ui.node.MeasureAndLayoutDelegate.doRemeasure(MeasureAndLayoutDelegate.kt:177)
        at androidx.compose.ui.node.MeasureAndLayoutDelegate.remeasureAndRelayoutIfNeeded(MeasureAndLayoutDelegate.kt:228)
        at androidx.compose.ui.node.MeasureAndLayoutDelegate.access$remeasureAndRelayoutIfNeeded(MeasureAndLayoutDelegate.kt:38)
        at androidx.compose.ui.node.MeasureAndLayoutDelegate.measureAndLayout(MeasureAndLayoutDelegate.kt:201)
        at androidx.compose.ui.platform.AndroidComposeView.measureAndLayout(AndroidComposeView.android.kt:662)
        at androidx.compose.ui.platform.AndroidComposeView.handleMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1073)
        at androidx.compose.ui.platform.AndroidComposeView.dispatchTouchEvent(AndroidComposeView.android.kt:1059)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3594)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3594)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3594)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3594)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:913)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1957)
        at android.app.Dialog.dispatchTouchEvent(Dialog.java:1162)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:871)
        at android.view.View.dispatchPointerEvent(View.java:15458)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:7457)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:7233)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6595)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6652)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6618)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:6786)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6626)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:6843)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6599)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6652)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6618)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6626)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6599)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:9880)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:9718)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:9671)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:10014)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:220)
        at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
        at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:200)
        at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:9960)
        at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:10056)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1010)
        at android.view.Choreographer.doCallbacks(Choreographer.java:809)
        at android.view.Choreographer.doFrame(Choreographer.java:737)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:995)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8595)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
I/Process: Sending signal. PID: 22418 SIG: 9

This is the AlertDialog code:

AlertDialog(
    onDismissRequest = { showDialog.value = false },
    modifier = Modifier.fillMaxHeight(.80f),
    text = {
        LazyColumn{
            items(30){ i ->
                Text(text = i.toString())
            }
        }
    },
    buttons = {
            Button(onClick = { /*TODO*/ }) {
                Text(text = "Cancel")
            }
    }
)
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
arieliito
  • 157
  • 2
  • 9
  • which compose version you are using? – Linh Jan 26 '22 at 02:17
  • I was able to reproduce it with `androidx.compose.material:material:1.2.0-alpha01`. Please [report](https://issuetracker.google.com/issues/new?component=612128) it to compose issue tracker as it's a bug. – Phil Dukhov Jan 26 '22 at 02:42
  • Using androidx.compose.material:material:1.1.0-rc01, thought it was an error, reported. – arieliito Jan 26 '22 at 22:01

2 Answers2

1

The same problem happens to me although I found that if I use the Material3 library scrolling is not a problem anymore:

implementation "androidx.compose.material3:material3:1.0.0-alpha03"

AlertDialog(
        onDismissRequest = { },
        modifier = Modifier.fillMaxHeight(0.8f),
        text = {
            LazyColumn() {
                items(30) { i ->
                    Text(text = i.toString())
                }
            }
        },
        dismissButton = {
            Button(onClick = { /*TODO*/ }) {
                Text(text = "Cancel")
            }
        }
    )

Make sure to import:

import androidx.compose.material3.AlertDialog

I don't know if this is the correct answer but I hope it helps in any case.

Code Poet
  • 6,222
  • 2
  • 29
  • 50
0

Another workaround is the usage of the base androidx.compose.ui.window.Dialog. This Dialog works without any crashes when using the LazyColumn:

@Composable
@Preview(showBackground = true)
fun SimpleDialog(onDismissRequest: () -> Unit = {}) {
    Dialog(
        onDismissRequest = onDismissRequest,
        content = {
            Surface(shape = RoundedCornerShape(8.dp)) {
                Column(
                    modifier = Modifier.padding(8.dp),
                ) {
                    Text(text = "Simple Dialog", style = MaterialTheme.typography.h6)
                    LazyColumn(
                        modifier = Modifier.height(150.dp),
                        content = {
                            items(30){ i ->
                                Text(text = i.toString())
                            }
                        }
                    )
                    Button(onClick = onDismissRequest) {
                        Text("Cancel")
                    }
                }
            }
        },
    )
}

enter image description here

user1185087
  • 4,468
  • 1
  • 30
  • 38