查找视图ById,用于返回 null 的多个文本字段



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

我有许多文本字段,我想将它们放入数组中,以便它们可以按顺序更新。当我尝试添加它们时,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"/>

你应该做:

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)

我想你想一个接一个地显示玩家得分。为此,我建议您使用回收器视图。这将是一个最佳实践,或者通过程序使用文本视图创建一个布局,并将其放入 numberOfPlyer 列表的循环中

最新更新