Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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
处理程序中未更新变量。post';Android中的run方法_Android_Multithreading_Handler_Runnable - Fatal编程技术网

处理程序中未更新变量。post';Android中的run方法

处理程序中未更新变量。post';Android中的run方法,android,multithreading,handler,runnable,Android,Multithreading,Handler,Runnable,我创建了一个新线程来处理我的TCP管理需求。我这样做是为了让UI线程在执行各种套接字魔术时保持活动状态。我遇到的问题是,ServerThread中使用的变量num似乎没有在handler.post()的run方法中更新。它只更改一次,但我的while(true)循环中的后续迭代不再更改其值。但是,在handler.post()之外,我注意到它正在正确地更改。为了查看num的值,我为logcat提供了一个log命令,这就是为什么我知道这是正在发生的事情。问题是,我需要run()方法中的变量num来

我创建了一个新线程来处理我的TCP管理需求。我这样做是为了让UI线程在执行各种套接字魔术时保持活动状态。我遇到的问题是,ServerThread中使用的变量num似乎没有在handler.post()的run方法中更新。它只更改一次,但我的while(true)循环中的后续迭代不再更改其值。但是,在handler.post()之外,我注意到它正在正确地更改。为了查看num的值,我为logcat提供了一个log命令,这就是为什么我知道这是正在发生的事情。问题是,我需要run()方法中的变量num来更新UI线程中的一些内容

可能是我声明num的方式有问题。我没有在代码中包含我认为与我的问题无关的大部分内容,因此如果我遗漏了一些内容,请立即告诉我。这是我第一次尝试android java线程,因此非常感谢您的帮助。只是今晚精神能量耗尽了

public class MainActivity extends Activity {
    private int num = 0;
    private Handler handler = new Handler();
    //Other declarations


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        //Do bunch of things including starting ServerThread
        //and creating a TCP server socket
        }

    public class ServerThread implements Runnable{
       public void run(){
       // ...
       // Do bunch of stuff including waiting for a client to connect
       try{                         
         BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
         while(true){ 
           num = in.read();
           Log.e("MainActivity", "A:" +num);       //<-- WORKS FINE
           if(num == -1) //client socket closed
             break;
           handler.post(new Runnable() {
              @Override
              public void run() {
                 Log.e("MainActivity", "B:" +num); //<-- DOES NOT WORK FINE
                 //Do bunch of stuff here with the variable num 
                 //including updating the UI thread
              }
           }
       }catch(Exception e){
              // ...
       }
    }
}
公共类MainActivity扩展活动{
私有int num=0;
私有处理程序=新处理程序();
//其他声明
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//做很多事情,包括启动ServerThread
//以及创建TCP服务器套接字
}
公共类ServerThread实现可运行{
公开募捐{
// ...
//做很多事情,包括等待客户端连接
试试{
BufferedReader in=新的BufferedReader(新的InputStreamReader(client.getInputStream());
虽然(正确){
num=in.read();

Log.e(“MainActivity”,“A::+num);//尝试将
num
变量声明为
volatile
是的,我已经尝试了
volatile
。没有工作。好的,然后尝试在
while
循环中声明另一个变量,并在
可运行的
对象中使用它。
final int newNum=num;
Log.e(“MainActivity”、“B:+newNum);
我有完全相同的问题。声明
volatile
对我也不起作用。