Android布局
- 接下来,将文本转换为十进制数字。对
stringInTextField调用toDouble(),并将其存储在一个名为cost的变量中。
val cost = stringInTextField.toDouble()不过,这样行不通 - 需要对
String调用toDouble()。原来EditText的text属性是Editable,因为它表示可以更改的文本。幸好,您可以通过对Editable调用toString()来将其转换为String。- 对
binding.costOfService.text调用toString()以将其转换为String:
val stringInTextField = binding.costOfService.text.toString()- 接下来,将文本转换为十进制数字。对
-
异常是系统提示出现问题的一种方式。在本例中,问题是
toDouble()无法将空String转换为Double。虽然EditText具有inputType=numberDecimal设置,但仍有可能输入一些toDouble()无法处理的值,如一个空字符串。了解 null
对空字符串或不表示有效十进制数字的字符串调用
toDouble()不起作用。幸好,Kotlin 还提供了一种名为toDoubleOrNull()的方法来处理这些问题。如果可以,它会返回十进制数字;如果存在问题,它会返回null。null 是一个特殊值,表示“无值”。它与值为
0.0的Double或包含零个字符串的空String""不同。Null表示没有值,没有Double或没有String。许多方法都期望有一个值,但它们可能不知道如何处理null并且会停止,这意味着应用会崩溃,因此 Kotlin 试图限制使用null的位置。您将在未来的课程中详细了解这一点。应用可以检查是否从
toDoubleOrNull()返回null,并在返回 null 时以不同的方式处理,这样应用就不会崩溃。 - IDE配置文件,gradal.propertitize 路径为TRUE
-
出错场景
运行一个老项目,编译时报以下错误Execution failed for task ':app:processDebugMainManifest'.
> Manifest merger failed : android:exported needs to be explicitly specified for <activity>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
1
2
3
4
5
解决方案
根据错误提示,targetSdkVersion大于等于SDK 31(也就是Android 12)时,如果有的Activity配置了Intent-filter,必须也同时配置exported属性,否则编译失败。所以我们有2种解决方案
降低targetSdkVersion为30.
defaultConfig {
applicationId "com.example.app"
minSdkVersion 21
//targetSdkVersion 31
targetSdkVersion 30
}
将Manifest中有配置Intent-filter的Activity加上android:exported属性
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:launchMode="standard"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> - 返回步骤跳过,直接回到标题,游戏开始页面
管理返回堆栈,以使用户在 GameWon 或 GameOver 屏幕中按返回按钮时会返回标题屏幕。您可以通过为连接 fragment 的操作设置“弹出”行为来管理返回堆栈:
- 操作的
popUpTo属性会在导航之前将返回堆栈“弹出”到给定的目的地。(系统会从返回堆栈中移除目的地。) - 如果
popUpToInclusive属性为false或未设置,则popUpTo会移除直到指定目的地的目的地,但会将指定目的地留在返回堆栈中。 - 如果
popUpToInclusive设置为true,则popUpTo属性会从返回堆栈中移除直到并包括给定目的地在内的所有目的地。 - 如果
popUpToInclusive为true且popUpTo设置为应用的起始位置,则操作会从返回堆栈中移除所有应用目的地。用户按返回按钮后会直接退出应用。
在此步骤中,您将为在上一项任务中创建的两项操作设置 popUpTo 属性。您将使用布局编辑器的 Attributes 窗格中的 Pop To 字段来执行此操作。
- 在 res > navigation 文件夹中打开
navigation.xml。如果布局编辑器中未显示导航图,请点击 Design 标签页。 - 选择从
gameFragment导航到gameOverFragment的操作。(在预览区域中,该操作由一条连接这两个 fragment 的蓝线表示。) - 在 Attributes 窗格中,将 popUpTo 设置为
gameFragment。选中 popUpToInclusive 复选框。

- 这样会在 XML 中设置
popUpTo和popUpToInclusive属性。这些属性会告知导航组件从返回堆栈中移除直到并包括GameFragment在内的 fragment。(这样做与将 popUpTo 字段设置为titleFragment并清除 popUpToInclusive 复选框的效果相同。
- link error 招不到color,material库版本太高,调低就可以
浙公网安备 33010602011771号