TextView的字符串设置错误 - 我该如何解决?

时间:2017-06-05 17:47:29

标签: java android

我和android studio一起工作了大约五个星期。在这个时候,我学会了如何设置TextView的字符串并设置包含该字符串的字符串变量。我一直在尝试为Android设备制作应用程序游戏,并在更改某些代码时遇到问题。

textView myText应该指定要按的彩色按钮,但是当我按下android模拟器中的开始按钮时,随机生成的文本和按钮永远不会相互关联,即使我有一系列if语句到确保他们这样做。

由于某种原因,当我运行代码时,第一个颜色按钮始终是正确的按钮,然后运行方法colourSwitch,如果它是正确的,它应该,但我不知道连接正确的彩色按钮有什么问题正确的文字。哦,我正在将此活动与丢失的游戏活动联系起来,当按下任何错误的按钮时,该活动会被调用。如果您想要其他活动的代码,我很乐意展示。

以下是我的游戏活动的xml文件:

ip -o addr show | egrep -i "ether|inet" 

这是我的游戏活动Java文件。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/game_activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.a2118667.red.MainActivity"
    tools:layout_editor_absoluteX="0dp"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/indicatorText">

    <Button
        android:id="@+id/startButton"
        android:layout_width="175dp"
        android:layout_height="75dp"
        android:layout_marginTop="8dp"
        android:text="@string/Game_Start_Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.156" />

    <GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="299dp"
        android:layout_height="301dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.505"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.88">

        <Button
            android:id="@+id/button1"
            android:layout_width="150dp"
            android:layout_height="150dp" />

        <Button
            android:id="@+id/button3"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_column="0"
            android:layout_row="1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_column="1"
            android:layout_row="0" />

        <Button
            android:id="@+id/button4"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_column="1"
            android:layout_row="1" />
    </GridLayout>

    <TextView
        android:id="@+id/indicatorText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:text=" "
        android:textAppearance="@style/TextAppearance.AppCompat.Button"
        android:textSize="34sp"
        app:layout_constraintBottom_toTopOf="@+id/startButton"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>

如果有人能弄清楚为什么我的代码表现得像这样,那将是值得赞赏的。

1 个答案:

答案 0 :(得分:0)

我找到了答案。你看到我的牛仔,而cowBears的整数被随机化了。

cowBears,cowBears2,cowbears和cowbears2最终整数必须直接在onCreate方法之上声明,因为大写cowBear整数在按下按钮时被重新声明。