我正在尝试测试使用 Kotlin 和 Jetpack compose 构建的 Android 应用程序以进行配置更改,例如切换到横向/纵向模式。当我将方向设置为横向/纵向时,我看到崩溃。 下面是简单的AndroidTest代码。有人可以帮我解决这个问题吗?
package com.example.sandbox
import android.content.pm.ActivityInfo
import androidx.compose.ui.test.ComposeTimeoutException
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.test.core.app.ActivityScenario
import org.junit.Rule
import org.junit.Test
class MainActivityTest {
@get:Rule
val rule = createAndroidComposeRule<MainActivity>()
private lateinit var scenario: ActivityScenario<MainActivity>
@Test
fun sandboxTest() {
scenario = ActivityScenario.launch(MainActivity::class.java)
rule.waitForIdle()
rule.activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
SandBoxTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Greeting(
name = "Android",
modifier = Modifier.padding(innerPadding)
)
}
}
}
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!"
)
}
崩溃:
java.lang.NullPointerException: Cannot run onActivity since Activity has been destroyed already
at androidx.test.internal.util.Checks.checkNotNull(Checks.java:50)
at androidx.test.core.app.ActivityScenario.lambda$onActivity$2$androidx-test-core-app-ActivityScenario(ActivityScenario.java:792)
at androidx.test.core.app.ActivityScenario$$ExternalSyntheticLambda2.run(D8$$SyntheticClass:0)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:487)
at java.util.concurrent.FutureTask.run(FutureTask.java:264)
at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:2497)
at android.os.Handler.handleCallback(Handler.java:984)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loopOnce(Looper.java:238)
at android.os.Looper.loop(Looper.java:357)
at android.app.ActivityThread.main(ActivityThread.java:8149)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:957)
您面临的问题可能是由于方向改变的时间造成的。当您设置请求的方向时,活动将被重新创建,这可能会导致您看到的崩溃。以下是帮助您解决此问题的几个步骤:
直接在测试规则中使用 ActivityScenario:无需手动创建和管理 ActivityScenario,而是在 Compose 测试规则中使用它。这可确保正确的同步和生命周期管理。
更改方向后等待 Activity 空闲:确保测试在更改方向后等待 Activity 空闲。
以下是更新测试代码的方法:
class MainActivityTest {
@get:Rule
val rule = createAndroidComposeRule<MainActivity>()
@Test
fun sandboxTest() {
// Launch the activity
val scenario = ActivityScenario.launch(MainActivity::class.java)
// Wait for the activity to be idle
rule.waitForIdle()
// Change the orientation to landscape
scenario.onActivity { activity ->
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}
// Wait for the activity to be idle again
rule.waitForIdle()
}
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Greeting(
name = "Android",
modifier = Modifier.padding(innerPadding)
)
}
}
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!"
)
}
在此更新的代码中:
这应该有助于防止在测试仍在运行时由于活动被破坏而导致 NullPointerException。