我卡在这里,解决不了,努力了好久。
我想在 Wear os Projet 中使用导航,但无法正确导入。
这是我的文件:
/* While this template provides a good starting point for using Wear Compose, you can always
* take a look at https://github.com/android/wear-os-samples/tree/main/ComposeStarter to find the
* most up to date changes to the libraries and their usages.
*/
package com.example.myapplicationwatch.presentation
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Devices
import androidx.compose.ui.tooling.preview.Devices.WEAR_OS_LARGE_ROUND
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.compose.NavHost NAVIGATION IN RED
import androidx.navigation.compose.rememberNavController NAVIGATION IN RED
import androidx.wear.compose.material.Button
import androidx.wear.compose.material.MaterialTheme
import androidx.wear.compose.material.Text
import androidx.wear.compose.material.TimeText
import androidx.wear.tooling.preview.devices.WearDevice
import androidx.wear.tooling.preview.devices.WearDevices
import com.example.myapplicationwatch.R
import com.example.myapplicationwatch.presentation.theme.MyApplicationWatchTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)
setTheme(android.R.style.Theme_DeviceDefault)
setContent {
MainScreen()
}
}
}
@Composable
fun MainScreen () {
Box(
modifier = Modifier
.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Column {
Button(
onClick = {},
modifier = Modifier
.size(120.dp, 50.dp),
shape = RoundedCornerShape(8.dp)
) { Text("Patinage libre") }
Spacer(modifier = Modifier.height(16.dp))
Button(
onClick = {},
modifier = Modifier
.size(120.dp, 50.dp),
shape = RoundedCornerShape(8.dp)
) { Text("Match") }
}
}
}
@Preview(device = WearDevices.LARGE_ROUND, showSystemUi = true)
@Composable
fun MainScrenPreview() {
MainScreen()
}
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.rememberNavController
当我将鼠标悬停在导入中的红色导航词上时,它显示:“未解析的引用”
Android Studio 建议我使用 Alt Shift Enter 导入依赖项,我这样做了,但没有成功。
在我的 gradle 中,我确实导入了导航依赖项,如您所见。
implementation(libs.androidx.navigation.compose)
我尝试重新启动计算机,检查更新,但似乎没有任何效果。这是非常令人沮丧的。
如何修复这两个导航导入?
谢谢
下面是我的 gradle:
------------------------------------------------- -------------------------------------------------- ---------
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.compose)
}
android {
namespace = "com.example.myapplication"
compileSdk = 34
defaultConfig {
applicationId = "com.example.myapplication"
minSdk = 28
targetSdk = 34
versionCode = 1
versionName = "1.0"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}
buildFeatures {
compose = true
}
}
dependencies {
implementation(libs.play.services.wearable)
implementation(platform(libs.compose.bom))
implementation(libs.ui)
implementation(libs.ui.graphics)
implementation(libs.ui.tooling.preview)
implementation(libs.compose.material)
implementation(libs.compose.foundation)
implementation(libs.wear.tooling.preview)
implementation(libs.activity.compose)
implementation(libs.core.splashscreen)
androidTestImplementation(platform(libs.compose.bom))
androidTestImplementation(libs.ui.test.junit4)
debugImplementation(libs.ui.tooling)
debugImplementation(libs.ui.test.manifest)
}
确保您的
toml
文件中存在与您的 build.gradle
条目相对应的条目。
implementation(libs.androidx.navigation.compose)
在您的
build.gradle
中应该指向条目
[versions]
//...
navigationCompose = "2.8.5"
[libraries]
//...
androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "navigationCompose" }
在您的
libs.versions.toml
文件中。