Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.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/8/magento/5.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
Android:在处理程序中更新当前活动UI_Android_Handler - Fatal编程技术网

Android:在处理程序中更新当前活动UI

Android:在处理程序中更新当前活动UI,android,handler,Android,Handler,当处理程序调用其他类时,我很难在活动中更新UI。下面是我创建的代码 活动A @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_A); Handler handler = new Handler(); //Here is where the handler is

当处理程序调用其他类时,我很难在活动中更新UI。下面是我创建的代码

活动A

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_A);

    Handler handler = new Handler(); //Here is where the handler is start, other class will update the Handler message.
    ......
}
活动B

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_B);

    ......
}

//Just a dummy function called when handler is triggered
private void onChange(){
    TextView tv_status = findViewById(R.id.status);
    tv_status.setText("Complete");
}
类处理程序

public Handler {

    public Handler(){
         Handler handler = new Handler(){
               public void HandleMessage(Message msg){
                    switch (msg.what) {
                        case 1:
                             break;
                        case 2:
                             //should trigger to update the TextView in Activity B here
                             break;
                   }
              }
        };

        .......

    }
}
活动A作为主处理程序,在这里调用处理程序来启动处理程序,活动B只是更新处理程序触发的UI。

希望这会有所帮助

public Handler {

    public Handler(){
         Handler handler = new Handler(){
               public void HandleMessage(Message msg){
                    switch (msg.what) {
                        case 1:
                             break;
                        case 2:
                             //should trigger to update the TextView in Activity A here 
                            runOnUiThread(new Runnable() {
                             @Override
                               public void run() {
                                 // Update your view
                               }
                              });
                             break;
                   }
              }
        };

        .......

    }
}

您的活动如何知道处理程序存在?您在哪里创建处理程序?要更新处理程序中的任何视图,您需要更新Main Thread.cricket_007中的视图。我的坏事业没有显示出完整的步骤。我会更新我的问题。对不起,兄弟,误解了,作为一个,你需要确保你在UI线程第一希望这个链接可以帮助你-