gpt4 book ai didi

java - textviewArray[i].setText(stringArray[i]);一片空白

转载 作者:行者123 更新时间:2023-12-02 00:58:07 25 4
gpt4 key购买 nike

我创建了 TextView 数组并定义了数组中的每个元素,我得到了一个带有 Intent 的字符串数组,但是当我尝试使用通过 for 循环接收字符串数组的元素设置 textview 文本时,它返回 null这段代码有什么问题?我是java初学者,

public class dizilisActivity extends AppCompatActivity {
TextView player1, player2,player3,player4,player5,player6,player7,player8,player9,player10;
TextView[] players = {player1, player2,player3,player4,player5,player6,player7,player8,player9,player10};
String[] receivedNames;


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dizilis);
player1= (TextView) findViewById(R.id.player1);
player2= (TextView) findViewById(R.id.player2);
player3= (TextView) findViewById(R.id.player3);
player4= (TextView) findViewById(R.id.player4);
player5= (TextView) findViewById(R.id.player5);
player6 = (TextView) findViewById(R.id.player6);
player7 = (TextView) findViewById(R.id.player7);
player8 = (TextView) findViewById(R.id.player8);
player9 = (TextView) findViewById(R.id.player9);
player10 = (TextView) findViewById(R.id.player10);
Intent intent = getIntent();
receivedNames = intent.getStringArrayExtra("names");
for (int i = 0; i<receivedNames.length; ++i)
players[i].setText(receivedNames[i]);

}

}

和 logcat

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.easoft.aldimverdim.dizilisActivity.onCreate(dizilisActivity.java:33)

第 33 行是

players[i].setText(receivedNames[i];

最佳答案

TL;DR

1) 改变你的来自

TextView[] players = {player1, player2,player3,player4,player5,player6,player7,player8,player9,player10}

进入

TextView[] players;

2)移动初始化,所以之后

player10 = (TextView) findViewById(R.id.player10);

添加:

players = new TextView[]{player1, player2, player3, player4, player5, player6, player7, player8, player9, player10};

3) 当receivedNames将为null时,您可以添加额外的条件,例如:

// Check if receivedNames are NOT null
if (receivedNames != null) {
for (int i = 0; i < receivedNames.length; ++i) {
// here rest of codes
}
}

错误解释

1)线路:

TextView player1, player2,player3,player4,player5,player6,player7,player8,player9,player10;

导致声明了 10 个变量。它们都是nulls

2)所以在下一行:

TextView[] players = {player1, player2,player3,player4,player5,player6,player7,player8,player9,player10};

声明新数组并使用 null 值对其进行初始化,以便您的数组内部包含 10 个 null

[null, null, null, null, null, null, null, null, null, null]

3) 当在 onCreate() 中尝试调用 players[i].setText(...) 时,players[i] 具有 null 值。因此,您对 null 调用 setText。这就是崩溃的原因。

完整的工作代码:

public class MainActivity extends AppCompatActivity {
TextView player1, player2, player3, player4, player5, player6, player7, player8, player9,
player10;

TextView[] players;

String[] receivedNames;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dizilis);

player1 = findViewById(R.id.player1);
player2 = findViewById(R.id.player2);
player3 = findViewById(R.id.player3);
player4 = findViewById(R.id.player4);
player5 = findViewById(R.id.player5);
player6 = findViewById(R.id.player6);
player7 = findViewById(R.id.player7);
player8 = findViewById(R.id.player8);
player9 = findViewById(R.id.player9);
player10 = findViewById(R.id.player10);

players = new TextView[]{player1, player2, player3, player4, player5, player6, player7,
player8, player9, player10};

Intent intent = getIntent();
receivedNames = intent.getStringArrayExtra("names");

if (receivedNames != null) {
for (int i = 0; i < receivedNames.length; ++i) {
players[i].setText(receivedNames[i]);
}
}
}
}

提示#1

好的做法是从较短或“导致更多问题”中获取索引,因此您可以更改:

i < receivedNames.length

至:

i < players.length

当您从 getStringArrayExtra() 接收到较少数据时 = 没有任何反应(只是空 TextView)。

但是当您尝试分配给不存在的 TextView 或在不存在的数组中定位时=崩溃

提示#2

如果您遇到相同/类似的循环问题,您可以:

  • 使用断点(关于在调试模式下构建Android应用程序`)或
  • 添加简单的try-catch block 来检查哪个索引导致问题。例如:
for (int i = 0; i < receivedNames.length; ++i) {
try {
players[i].setText(receivedNames[i]);
} catch (Exception e) {
System.out.println("Problem with id = " + i);
}
}

关于java - textviewArray[i].setText(stringArray[i]);一片空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61061870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com