Java Android:相同的代码行不能在不同的方法中工作
如果我碰巧在构造被调用变量的同一个方法中使用了几行代码,那么它是有效的,但是如果我创建另一个方法,只执行相同的操作,复制粘贴,然后只调用其中的另一个方法,那么它就不起作用了Java Android:相同的代码行不能在不同的方法中工作,java,android,exception-handling,reference,Java,Android,Exception Handling,Reference,如果我碰巧在构造被调用变量的同一个方法中使用了几行代码,那么它是有效的,但是如果我创建另一个方法,只执行相同的操作,复制粘贴,然后只调用其中的另一个方法,那么它就不起作用了 private void initElements(){ TextView ageValue[] = { (TextView)this.findViewById(R.id.age1Value), (Tex
private void initElements(){
TextView ageValue[] =
{
(TextView)this.findViewById(R.id.age1Value),
(TextView)this.findViewById(R.id.age2Value),
(TextView)this.findViewById(R.id.age3Value)
};
TextView ageText[] =
{
(TextView)this.findViewById(R.id.age1Text),
(TextView)this.findViewById(R.id.age2Text),
(TextView)this.findViewById(R.id.age3Text)
};
for(int i = 0; i<=2;i++){
intValues[i] = a.getHours((i));
stringValues[i] = String.valueOf(intValues[i]);
ageValue[i].setText(stringValues[i]);
}
//updateValues();
}
private void updateValues(){
for(int i = 0; i<=2;i++){
intValues[i] = a.getHours((i));
stringValues[i] = String.valueOf(intValues[i]);
ageValue[i].setText(stringValues[i]);
}
}
对此
我尝试过重建、清理程序并重新安装。我在虚拟设备和真正的智能手机上试用过。一次又一次地犯同样的错误。(此外,当我悬停在图片中的错误上时,它有时会说“源代码与字节码不匹配”,但有时它也不匹配。”还到处尝试“this.”关键字,但没有成功
我只是对这怎么可能发生感到困惑。也许这个方法没有得到某种正确的内存引用,因为它不是直接在声明旁边,但我指的是“error”语句之前的两条语句“尽管它们具有相同的性质,但工作非常好。如果你能告诉我为什么这不起作用,那就太好了,谢谢
TextView ageValue[] = new TextView[3];
TextView ageText[] = new TextView[3];
int intValues[] = new int[3];
String stringValues[] = new String[3];
CalculateDates a = new CalculateDates();
这是已使用变量的声明。(在类的开头)您需要为该类使用全局ageValue[]和ageText[]
private void initElements(){
ageValue[0] =(TextView)this.findViewById(R.id.age1Value);
ageValue[1] =(TextView)this.findViewById(R.id.age2Value);
ageValue[2] =(TextView)this.findViewById(R.id.age3Value);
ageText[0] = (TextView)this.findViewById(R.id.age1Text);
ageText[1] = (TextView)this.findViewById(R.id.age2Text);
ageText[2] = (TextView)this.findViewById(R.id.age3Text);
for(int i = 0; i<=2;i++){
intValues[i] = a.getHours((i));
stringValues[i] = String.valueOf(intValues[i]);
ageValue[i].setText(stringValues[i]);
}
private void initElements(){
ageValue[0]=(TextView)this.findViewById(R.id.age1Value);
ageValue[1]=(TextView)this.findViewById(R.id.age2Value);
ageValue[2]=(TextView)this.findViewById(R.id.age3Value);
ageText[0]=(TextView)this.findViewById(R.id.age1Text);
ageText[1]=(TextView)this.findViewById(R.id.age2Text);
ageText[2]=(TextView)this.findViewById(R.id.age3Text);
对于(int i=0;i您需要为该类使用全局ageValue[]和ageText[]
private void initElements(){
ageValue[0] =(TextView)this.findViewById(R.id.age1Value);
ageValue[1] =(TextView)this.findViewById(R.id.age2Value);
ageValue[2] =(TextView)this.findViewById(R.id.age3Value);
ageText[0] = (TextView)this.findViewById(R.id.age1Text);
ageText[1] = (TextView)this.findViewById(R.id.age2Text);
ageText[2] = (TextView)this.findViewById(R.id.age3Text);
for(int i = 0; i<=2;i++){
intValues[i] = a.getHours((i));
stringValues[i] = String.valueOf(intValues[i]);
ageValue[i].setText(stringValues[i]);
}
private void initElements(){
ageValue[0]=(TextView)this.findViewById(R.id.age1Value);
ageValue[1]=(TextView)this.findViewById(R.id.age2Value);
ageValue[2]=(TextView)this.findViewById(R.id.age3Value);
ageText[0]=(TextView)this.findViewById(R.id.age1Text);
ageText[1]=(TextView)this.findViewById(R.id.age2Text);
ageText[2]=(TextView)this.findViewById(R.id.age3Text);
对于(int i=0;updatevalue()
中的ageValue
与您在initElements()
中声明和初始化的initElements()中声明和初始化的TextView ageValue[]
声明了一个新的、不同的变量,该变量具有与initElements()相同的本地名称
@MikeM。但是声明本身不在initElements()函数中。声明(请参见我的编辑)位于类的开头,还是我有什么地方出错?是否使用“this.”关键字帮助?是的。TextView ageValue[]
是一个声明。它与class字段不同,是一个单独的声明。@MikeM。所以我需要使用“this.”关键字来专门引用在这个实例中创建的ageValue[]吗?@MikeM。我甚至都没有想到。谢谢,伙计。updateValue()中的ageValue
)
与您在initElements()
中声明和初始化的ageValue
不同TextView ageValue[]
声明了一个新的、不同的变量,该变量的名称与initElements()
@MikeM()相同。但是声明本身不在initElements()函数中。声明(请参见我的编辑)是在类的开头,还是我弄错了什么?使用“this.”关键字是否有帮助?是的。TextView ageValue[]
是一个声明。它与类字段不同,是一个单独的声明。@MikeM。所以我需要使用“this.”关键字来专门指代ageValue[]在该实例中创建?@MikeM。我甚至没有想到lol。谢谢,伙计!>也不要在方法中声明TextView。|这是这里的主要问题。但是,是的,应该对特定实例使用全局变量。您在initElements()中创建了这些TextView的不同实例方法和全局TextView仍然为null。如果这起作用,请不要忘记接受它。“您在initElements()方法中创建了这些TextView的不同实例,而全局TextView仍然为null”将此添加到您的帖子中,然后我将接受。它比最初的第一行更好地描述了问题。我在回答中确实提到了这一点。>不要在方法中声明TextView。|这是这里的主要问题。但是,是的,应该对特定实例使用globals您在initEle中创建了这些TextView的不同实例方法和全局TextView仍然为null。如果这起作用,请不要忘记接受它。“您在initElements()方法中创建了这些TextView的不同实例,而全局TextView仍然为null”将此添加到您的帖子中,然后我将接受它。它比最初的第一行更好地描述了问题。我在回答中确实提到了这一点。