多个TextField的findViewById返回null

时间:2019-07-18 12:45:47

标签: android kotlin

我很欣赏这个问题被问过很多次-我只是无法为我的问题找到正确的解决方案。如果有人可以引导我找到所需的解决方案,我将不胜感激-我知道这可能是一个非常基本的问题。

我有许多要添加到数组中的textField,以便可以按顺序更新它们。当我尝试添加它们时,findViewById()函数将它们返回为null。下面是该活动的onCreate方法以及该活动的XML。

谢谢:-)

override fun onCreate(savedInstanceState: Bundle?) 
{
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_nuble_game)
    windowManager.defaultDisplay.getMetrics(displayMetrics)

    val playerList = intent.getStringArrayListExtra("Player_Array")
    numberOfPlayers = playerList.count()

    playerNameLabels.add(rl_nubble_game_layout.findViewById(R.id.player1NameText) as TextView)
    playerNameLabels.add(rl_nubble_game_layout.findViewById(R.id.player2NameText) as TextView)
    playerNameLabels.add(rl_nubble_game_layout.findViewById(R.id.player3NameText) as TextView)
    playerNameLabels.add(rl_nubble_game_layout.findViewById(R.id.player4NameText) as TextView)

    playerScoreLabels.add(rl_nubble_game_layout.findViewById(R.id.player1ScoreText) as TextView)
    playerScoreLabels.add(rl_nubble_game_layout.findViewById(R.id.player2ScoreText) as TextView)
    playerScoreLabels.add(rl_nubble_game_layout.findViewById(R.id.player3ScoreText) as TextView)
    playerScoreLabels.add(rl_nubble_game_layout.findViewById(R.id.player4ScoreText) as TextView)

    for (name in playerList)
    {
        var thisPlayer = Player(name, false)
        thisPlayer.score = 0
        playerArray.add(thisPlayer)
    }

    if (numberOfPlayers == 1)
    {
        numberOfPlayers = 2
        var computerPlayer = Player("Conan the Destroyer", true)
        playerArray.add(computerPlayer)
    }

    for (i in 0..numberOfPlayers - 1)
    {
        playerNameLabels[i].text = playerArray[i].name
    }
}

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_nuble_game"
    tools:context=".NubbleGameActivity">

<RelativeLayout
        android:id="@+id/rl_nubble_game_layout"
        android:layout_width="391dp"
        android:layout_height="663dp"
        android:orientation="vertical" tools:layout_editor_absoluteY="6dp" tools:layout_editor_absoluteX="4dp"/>
<TextView
        android:text="TextView"
        android:layout_width="162dp"
        android:layout_height="32dp"
        android:id="@+id/player1NameText" android:textSize="24sp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="16dp"
        android:layout_marginStart="28dp" app:layout_constraintStart_toStartOf="parent"/>
<TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/player1NameText"
        android:id="@+id/player1ScoreText" android:textSize="24sp"
        app:layout_constraintStart_toEndOf="@+id/player1NameText" android:layout_marginStart="32dp"
        android:layout_marginTop="16dp" app:layout_constraintTop_toTopOf="parent"/>
<TextView
        android:text="TextView"
        android:layout_width="165dp"
        android:layout_height="32dp"
        android:id="@+id/player2NameText" android:textSize="24sp"
        android:layout_below="@+id/player1NameText"
        android:layout_marginStart="28dp"
        app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="12dp"
        app:layout_constraintTop_toBottomOf="@+id/player1NameText" android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/player3NameText"/>
<TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/player2NameText"
        android:layout_below="@+id/player1ScoreText"
        android:id="@+id/player2ScoreText" android:textSize="24sp"
        app:layout_constraintStart_toEndOf="@+id/player2NameText"
        android:layout_marginStart="28dp" android:layout_marginTop="12dp"
        app:layout_constraintTop_toBottomOf="@+id/player1ScoreText"/>
<TextView
        android:text="TextView"
        android:layout_width="167dp"
        android:layout_height="32dp"
        android:id="@+id/player3NameText" android:textSize="24sp"
        android:layout_below="@+id/player2NameText"
        app:layout_constraintTop_toBottomOf="@+id/player2NameText" android:layout_marginBottom="8dp"
        tools:layout_editor_absoluteY="105dp" android:layout_marginStart="28dp"
        app:layout_constraintStart_toStartOf="parent"/>
<TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/player3NameText"
        android:layout_below="@+id/player2ScoreText"
        android:id="@+id/player3ScoreText" android:textSize="24sp"
        app:layout_constraintStart_toEndOf="@+id/player3NameText"
        android:layout_marginStart="24dp" android:layout_marginTop="12dp"
        app:layout_constraintTop_toBottomOf="@+id/player2ScoreText"/>
<TextView
        android:text="TextView"
        android:layout_width="165dp"
        android:layout_height="32dp"
        android:layout_below="@+id/player3NameText"
        android:id="@+id/player4NameText" android:textSize="24sp"
        android:layout_marginStart="28dp" app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="12dp" app:layout_constraintTop_toBottomOf="@+id/player3NameText"/>
<TextView
        android:text="TextView"
        android:layout_width="102dp"
        android:layout_height="32dp"
        android:layout_toRightOf="@+id/player4NameText"
        android:layout_below="@+id/player3ScoreText"
        android:id="@+id/player4ScoreText" android:textSize="24sp"
        app:layout_constraintStart_toEndOf="@+id/player4NameText" android:layout_marginStart="28dp"
        android:layout_marginTop="12dp" app:layout_constraintTop_toBottomOf="@+id/player3ScoreText"/>

2 个答案:

答案 0 :(得分:2)

您应该这样做:

playerNameLabels.add(findViewById(R.id.player1NameText) as TextView)
playerNameLabels.add(findViewById(R.id.player2NameText) as TextView)
playerNameLabels.add(findViewById(R.id.player3NameText) as TextView)
playerNameLabels.add(findViewById(R.id.player4NameText) as TextView)

playerScoreLabels.add(findViewById(R.id.player1ScoreText) as TextView)
playerScoreLabels.add(findViewById(R.id.player2ScoreText) as TextView)
playerScoreLabels.add(findViewById(R.id.player3ScoreText) as TextView)
playerScoreLabels.add(findViewById(R.id.player4ScoreText) as TextView)

答案 1 :(得分:0)

我认为您想一一展现球员得分。 我建议您为此使用回收者视图。这是最佳做法,或者通过程序使用textview创建布局,并将其放入numberOfPlyer列表的for循环中。