Java textviewArray[i].setText(stringArray[i]);是空的
我创建了TextView数组并定义了数组中的每个元素,我得到了一个字符串数组,但当我试图用接收到带有for循环的字符串数组的元素设置TextView文本时,它返回null 这些代码有什么问题?我是java和java的初学者Java textviewArray[i].setText(stringArray[i]);是空的,java,android,arrays,string,for-loop,Java,Android,Arrays,String,For Loop,我创建了TextView数组并定义了数组中的每个元素,我得到了一个字符串数组,但当我试图用接收到带有for循环的字符串数组的元素设置TextView文本时,它返回null 这些代码有什么问题?我是java和java的初学者 public class dizilisActivity extends AppCompatActivity { TextView player1, player2,player3,player4,player5,player6,player7,player8,player9
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]);
}
似乎您从未更新数组中的值。由于您知道阵列中将有10个视图,请尝试以下操作
private final TextView[]views=new TextView[10];
创建时受保护的void(Bundle savedInstanceState){
视图[0]=(TextView)findViewById(R.id.player1);
视图[1]=(TextView)findViewById(R.id.player2);
//……等等
}
似乎永远不会更新数组中的值。由于您知道阵列中将有10个视图,请尝试以下操作
private final TextView[]views=new TextView[10];
创建时受保护的void(Bundle savedInstanceState){
视图[0]=(TextView)findViewById(R.id.player1);
视图[1]=(TextView)findViewById(R.id.player2);
//……等等
}
TL;博士
1) 换衣服
从
进入
2) 移动初始化,所以之后
player10 = (TextView) findViewById(R.id.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
}
}
导致声明了10个变量。它们都是null
s
2) 因此,在下一行:
TextView[] players = {player1, player2,player3,player4,player5,player6,player7,player8,player9,player10};
声明新数组并用null
值初始化它,这样就可以在数组中包含10null
s
[null, null, null, null, null, null, null, null, null, null]
3) 在onCreate()
中尝试调用players[i].setText(…)
时,players[i]
具有null
值。因此,在null
上调用setText
。这就是崩溃的原因
完整工作代码:
TL;博士
1) 换衣服
从
进入
2) 移动初始化,所以之后
player10 = (TextView) findViewById(R.id.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
}
}
导致声明了10个变量。它们都是null
s
2) 因此,在下一行:
TextView[] players = {player1, player2,player3,player4,player5,player6,player7,player8,player9,player10};
声明新数组并用null
值初始化它,这样就可以在数组中包含10null
s
[null, null, null, null, null, null, null, null, null, null]
3) 在onCreate()
中尝试调用players[i].setText(…)
时,players[i]
具有null
值。因此,在null
上调用setText
。这就是崩溃的原因
完整工作代码:
似乎您首先使用文本视图的空引用初始化数组,然后不更新引用,比如
players[index]=(TextView)findViewById(R.id.player1)
。不要这样做,而是创建一个空数组
然后使用for循环动态获取文本视图id并将对象推送到数组中
public class dizilisActivity extends AppCompatActivity {
TextView[] players = new TextView[10];
String[] receivedNames;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dizilis);
for(int i=0;i<10;i++){
String textViewId="player"+(i+1);
int resID = getResources().getIdentifier(textViewId, "id", getPackageName());
players[i]=(TextView) findViewById(resID);
}
Intent intent = getIntent();
receivedNames = intent.getStringArrayExtra("names");
for (int i = 0; i<receivedNames.length; ++i)
players[i].setText(receivedNames[i]);
}
公共类dizilisActivity扩展了AppCompativity{
TextView[]玩家=新的TextView[10];
字符串[]接收名称;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dizilis);
对于(int i=0;i来说,您似乎是首先使用文本视图的空引用初始化数组,而不是在类似players[index]=(TextView)findViewById(R.id.player1)
之后更新那里的引用
然后使用for循环动态获取文本视图id并将对象推送到数组中
public class dizilisActivity extends AppCompatActivity {
TextView[] players = new TextView[10];
String[] receivedNames;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dizilis);
for(int i=0;i<10;i++){
String textViewId="player"+(i+1);
int resID = getResources().getIdentifier(textViewId, "id", getPackageName());
players[i]=(TextView) findViewById(resID);
}
Intent intent = getIntent();
receivedNames = intent.getStringArrayExtra("names");
for (int i = 0; i<receivedNames.length; ++i)
players[i].setText(receivedNames[i]);
}
公共类dizilisActivity扩展了AppCompativity{
TextView[]玩家=新的TextView[10];
字符串[]接收名称;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dizilis);
对于(int i=0;i请发布您收到receivedNames
的代码以及错误的屏幕截图。因为我觉得receivedName似乎是字符串,您不能在循环中使用它的长度`i请提供更多详细信息。展示您如何获得“receivedNames”数组i,如您所愿编辑onCreate()的全部内容
方法或整个活动。请发布您收到receivedNames
的代码以及错误的屏幕截图。因为我认为receivedName是字符串,您不能在循环中使用它的长度`i请提供更多详细信息。展示您如何获得“receivedNames”Array可根据您的意愿编辑onCreate()
方法或整个活动的全部内容。
i < receivedNames.length
i < players.length
for (int i = 0; i < receivedNames.length; ++i) {
try {
players[i].setText(receivedNames[i]);
} catch (Exception e) {
System.out.println("Problem with id = " + i);
}
}
public class dizilisActivity extends AppCompatActivity {
TextView[] players = new TextView[10];
String[] receivedNames;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dizilis);
for(int i=0;i<10;i++){
String textViewId="player"+(i+1);
int resID = getResources().getIdentifier(textViewId, "id", getPackageName());
players[i]=(TextView) findViewById(resID);
}
Intent intent = getIntent();
receivedNames = intent.getStringArrayExtra("names");
for (int i = 0; i<receivedNames.length; ++i)
players[i].setText(receivedNames[i]);
}