Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java/android代码中变量的范围。_Java_Android_Variables - Fatal编程技术网

java/android代码中变量的范围。

java/android代码中变量的范围。,java,android,variables,Java,Android,Variables,我的android代码如下所示: boolean b; Context context; //inside onCreate method: b =false; context= getApplicationContext(); //outside onCreate method public void c(){ new Thread(new Runnable(){ boolean d = false; public void run(){ //inside a try/catch b

我的android代码如下所示:

boolean b;
Context context;
//inside onCreate method:
b =false;
context= getApplicationContext();

//outside onCreate method
public void c(){

new Thread(new Runnable(){

boolean d = false;

public void run(){

//inside a try/catch block:
try{

d = true;
b =d; // b was defined inside onCreate

}catch{

} 

}
}).start();

if(b)Toast.makeText(context,"hello whale",Toast.LENGTH_LONG).show();
else Toast.makeText(context,"hello Lion",Toast.LENGTH_LONG).show();  


} //end of method c
打印的祝酒词是“你好,狮子”。我不知道为什么,因为在try/catch块中b被设置为true。我不知道会错过什么

我知道这很简单,但我不知道它是什么。我需要在手机屏幕上打印“hello whale”

我知道try/catch块的try部分正在执行,因为我有一个outputstream正在写入本地主机数据库。所有需要写入的字段都已被outputstream写入


感谢您的建议

不能保证runnable中的代码会在Toast块执行之前执行完毕。这只意味着线程在Toast块之前“启动”。如果你真的需要这个线程(我假设这是你试图用多个线程完成的事情的代理代码),在线程上使用join方法。想不出使用单个线程的理由

new Thread(new Runnable(){

boolean d = false;

public void run(){

//inside a try/catch block:
try{

d = true;
b =d; // b was defined inside onCreate

}catch{

} 
}
}).start().join();

使用断点检查b在查询之前是否实际设置为true。断点是什么意思@F43nd1rBreakpoints允许暂停代码的执行。你可以在你最喜欢的搜索引擎上找到很多。