Java 从不同类访问变量
有2个班;Java 从不同类访问变量,java,android,class,Java,Android,Class,有2个班;main活动和SimpleFLashLighImpl。 在MainActivity类中,我输入了一个代码以从编辑框“否”中获取字符串 然后将其转换为整数“intdelay”。 这是代码的主要活动部分: public static volatile int intdelay = 1000; textView=(TextView)findViewById(R.id.textView); delay=(EditText)findViewById(R.id.e
main活动
和SimpleFLashLighImpl
。
在MainActivity
类中,我输入了一个代码以从编辑框“否”中获取字符串
然后将其转换为整数“intdelay”。
这是代码的主要活动部分:
public static volatile int intdelay = 1000;
textView=(TextView)findViewById(R.id.textView);
delay=(EditText)findViewById(R.id.edttxt);
String no=delay.getText().toString(); //this will get a string
try{
MainActivity.intdelay = Integer.parseInt(no);
}catch(NumberFormatException ex){ // handle your exception
}
Button btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView.setText(delay.getText());
}
});
在另一个类中,我尝试访问'intdelay'int值,以使用处理程序进行延迟。
这是代码的另一个类部分:
public void switchFlash() {
final Runnable runnable = new Runnable() {
@Override
public void run() {
if (isFlashOn()) {
turnOffFlash();
x++;
} else if (x>10) {
turnOffFlash();
}
else
{
turnOnFlash();
}
handler.postDelayed(this, MainActivity.intdelay);
}
};
handler.postDelayed(runnable,MainActivity.intdelay);
}
但问题是,每次我得到的延迟都是1000,这是在MainActivity
类开始时初始化的。
解决办法是什么 您正在创建一个新的
intdelay
变量,而不是修改声明的静态变量:
int intdelay = Integer.parseInt(no);
应该是
MainActivity.intdelay = Integer.parseInt(no);
一旦解决了这个问题,您将遇到内存可见性问题。除非使用某种形式的同步,否则不能保证一个线程所做的更改对其他线程可见
我建议将intdelay
变量标记为volatile
:
public static volatile int intdelay = 1000;
您的代码可能正在抛出异常并进入catch语句,所以默认值仍然是1000。检查你的
int intdelay = Integer.parseInt(no);
使用logd/toast检查您试图解析的值。这样地
如果在活动课上
try{
int intdelay = Integer.parseInt(no);
}catch(NumberFormatException ex){ // handle your exception
Toast.makeText(MainActivity.this(),"no is: "+ no,Toast.LENGTH_SHORT).show();
}
为什么不在runnable中声明延迟? 我的答案是关于可运行的post循环 例如:
final Handler handler = new Handler();
handler.post(new Runnable() {
// this int will also be passed to method post delayed
// as "this" keyword applies to Anonymous Class
// which body contains everything between brackets of new Runnable() { ... }
int withThisDelay = 1000;
@Override
public void run() {
handler.postDelayed(this,withThisDelay);
withThisDelay += 1000;
}
});
您可以将runnable扩展(实现)为自己的类(抽象而不运行)(使用getter/setter),然后将这个runnable定义为变量并使用它
public abstract class MyIntRunnable implements Runnable {
int _myDelay = 1000;
// to get delay
public int getDelay() { return _myDelay; }
// to set delay
public void setDelay(int myDelay) { _myDelay = myDelay; }
}
然后使用:
ClassA
private final static MyRunnable myRunnable = new MyRunnable() {
@Override
public void run() {
// post with var delayed
Handler.postDelayed(this,getDelay())
}
}
// get delay from runnable
myRunnable.setDelay(2000);
// set delay to runnable
int myDelayFromRunnable = myDelay.getDelay();
ClassB
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// get string from edit text
String getDelayString = delay.getText();
// prevent null values or empty string
// you should use catch numberformat exception here
// this mean check if text is not null and is not empty
// if empty or null set to 1000 else try parse to int
int delay = getDelayString != null && !getDelayString.isEmpty()
? Integer.parseImnt(getDelayString) : 1000;
// set delay in static runnable in other class
ClassA.myRunnable.setDelay(delay);
}
});
NumberFormatException的示例使用捕获
EditText editText = (EditText) findViewById(.....);
// we can define this as method and use in on click listener
private void myParseMethod(EditText editText) {
try {
String stringDelay = editText.getText().toString();
// if parsing was successful
ClassA.myRunnable.setDelay(stringDelay);;
} catch (NumberFormatException nxe) {
// inform user
editText.setError("Bad integer!!!");
}
}
您永远不会更改intdelay的值
MainActivity.intdelay=Integer.parseInt(否)代码>不进行比较,不进行更改。使用==
设置新值。
您还应该将变量声明为私有,并构建getter/setter方法。
你也应该考虑把延迟变量移到可运行的地方,如果不需要其他地方。没有改变类变量<代码> ItList< <代码>的语句,所以它总是停留在初始值1000。可能语句int intdelay=Integer.parseInt(no)
的本意是更改它,但它引入了一个块局部变量,使类变量保持不变。但是,即使您删除了类型声明,它也可能不会执行您想要的操作,因为该操作似乎发生在按钮的onClick
方法中,在该方法中,您再次不接触类变量。@Alowiwi您可以用更新的代码更新您的问题吗?好的,看起来不错,应该可以。您能否检查以下代码是否正在被调用,以及您的文本输入是否正在更新intdelay
:MainActivity.intdelay=Integer.parseInt(否)代码>?如何检查?我对android不太了解,但您应该能够在该语句前后记录一条消息:MainActivity.intdelay=Integer.parseInt(否)编码>然后使用adb logcat查看输出?还要记录从编辑框中获取的no
的值。然后将getContext()替换为您的活动名称,如main activity()。这会导致应用程序在启动前崩溃。请重新复制代码并检查“no”的值,您获取的是字符串、整数还是长类型?这个问题解决了吗?那么请把它标记为已解决。