Android布局

    1. 接下来,将文本转换为十进制数字。对 stringInTextField 调用 toDouble(),并将其存储在一个名为 cost 的变量中。
    val cost = stringInTextField.toDouble()
    

    不过,这样行不通 - 需要对 String 调用 toDouble()。原来 EditText 的 text 属性是 Editable,因为它表示可以更改的文本。幸好,您可以通过对 Editable 调用 toString() 来将其转换为 String

    1. 对 binding.costOfService.text 调用 toString() 以将其转换为 String
    val stringInTextField = binding.costOfService.text.toString()
  1. 异常是系统提示出现问题的一种方式。在本例中,问题是 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 时以不同的方式处理,这样应用就不会崩溃。

  2. IDE配置文件,gradal.propertitize  路径为TRUE
  3. 出错场景
    运行一个老项目,编译时报以下错误

    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>

  4. 返回步骤跳过,直接回到标题,游戏开始页面

管理返回堆栈,以使用户在 GameWon 或 GameOver 屏幕中按返回按钮时会返回标题屏幕。您可以通过为连接 fragment 的操作设置“弹出”行为来管理返回堆栈:

  • 操作的 popUpTo 属性会在导航之前将返回堆栈“弹出”到给定的目的地。(系统会从返回堆栈中移除目的地。)
  • 如果 popUpToInclusive 属性为 false 或未设置,则 popUpTo 会移除直到指定目的地的目的地,但会将指定目的地留在返回堆栈中。
  • 如果 popUpToInclusive 设置为 true,则 popUpTo 属性会从返回堆栈中移除直到并包括给定目的地在内的所有目的地。
  • 如果 popUpToInclusive 为 true 且 popUpTo 设置为应用的起始位置,则操作会从返回堆栈中移除所有应用目的地。用户按返回按钮后会直接退出应用。

在此步骤中,您将为在上一项任务中创建的两项操作设置 popUpTo 属性。您将使用布局编辑器的 Attributes 窗格中的 Pop To 字段来执行此操作。

  1. 在 res > navigation 文件夹中打开 navigation.xml。如果布局编辑器中未显示导航图,请点击 Design 标签页。
  2. 选择从 gameFragment 导航到 gameOverFragment 的操作。(在预览区域中,该操作由一条连接这两个 fragment 的蓝线表示。)
  3. 在 Attributes 窗格中,将 popUpTo 设置为 gameFragment。选中 popUpToInclusive 复选框。

7a65cf0c73c9904d.png

  1. 这样会在 XML 中设置 popUpTo 和 popUpToInclusive 属性。这些属性会告知导航组件从返回堆栈中移除直到并包括 GameFragment 在内的 fragment。(这样做与将 popUpTo 字段设置为 titleFragment 并清除 popUpToInclusive 复选框的效果相同。

 

  1. link error 招不到color,material库版本太高,调低就可以
posted @   SunShine789  阅读(21)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示