Nurseyit Tursunkulov
Nurseyit Tursunkulov

Reputation: 9400

Compose java.lang.IllegalStateException: pending composition has not been applied

I just want to run the simple test

class Exa {
@get:Rule
val composeTestRule = createAndroidComposeRule<MainActivity>()
// createComposeRule() if you don't need access to the activityTestRule

@Test
fun MyTest() {
    // Start the app
    composeTestRule.setContent {
            Greeting2("Nurs")
    }
    
    composeTestRule.onNodeWithText("Hello Nurs!").assertIsDisplayed()
}
}

@Composable
fun Greeting2(name: String) {
 Text(text = "Hello $name!")
}

but it gives me the following error:

java.lang.IllegalStateException: pending composition has not been applied

the strange thing that if I run it in another project it works

Upvotes: 4

Views: 3135

Answers (4)

Shreyash.K
Shreyash.K

Reputation: 1148

If you are using Canvas Composable then give it some size. I was facing the same issue which was resolved after setting a fixed size.

    Canvas(modifier = Modifier.size(400.dp)) {
    ...
    }

You may use fillMaxWidth() or something similar.

From Documentation :

Canvas Composable is a component that allow you to specify an area on the screen and perform canvas drawing on this area. You MUST specify size with modifier, whether with exact sizes via Modifier.size modifier, or relative to parent, via Modifier.fillMaxSize, ColumnScope.weight, etc. If parent wraps this child, only exact sizes must be specified.

Upvotes: 0

Renattele Renattele
Renattele Renattele

Reputation: 2286

This happend to me when I put images to my drawable folder from the outside. In a release variant of app I had the same exception, but not in debug one. I just cleaned project(Build -> Clean Project) and everything worked fine.

Upvotes: 0

wshelor
wshelor

Reputation: 383

Wanted to add some detail for anyone who comes here from Google. The exception listed in the title:

java.lang.IllegalStateException: pending composition has not been applied

is thrown if a Composable throws an exception while composing or recomposing the view. In the case that the original poster was noticing, the exception was likely thrown because the activity itself was not able to host the composition, but other code may cause this if an exception is thrown within a composable method.

Upvotes: 3

Nurseyit Tursunkulov
Nurseyit Tursunkulov

Reputation: 9400

the problem was in MainActivity:

class MainActivity3 :
ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
            // A surface container using the 'background' color from the theme
            Surface(color = MaterialTheme.colors.background) {
                Greeting("Android")
            }
    }
}
} 

@Composable
fun Greeting(name: String) 

you have to use clear activity without any dependency

Upvotes: -2

Related Questions