java - 试图在线程中按下PopupWindow的按钮

标签 java android multithreading onclicklistener popupwindow

我有一个尝试连接到服务器的客户端。如果连接失败,则在5秒钟后超时。我想打开一个没有连接符号和“重试连接”按钮的弹出窗口。弹出窗口是在线程中创建的,因此,如果连接失败,线程将结束。因此,我在主要 Activity 中加入了click方法。为什么不起作用?

连接失败后线程运行的代码:

((Activity) context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                LayoutInflater inflater = (LayoutInflater)
                        ((Activity) context).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final PopupWindow noConnection = new PopupWindow(
                        inflater.inflate(R.layout.popup_no_connection, null, false),
                        800,
                        800,
                        true);
                noConnection.setAnimationStyle(R.style.AnimationFade);
                noConnection.showAtLocation(((Activity) context).findViewById(R.id.entrance_layout), Gravity.CENTER, 0, 0);
                globalClass.getErrorHandler().setNoConnectionWithServer(noConnection);
            }
        });

主要 Activity 中的代码:
((GlobalClass) getApplicationContext()).initiateClass();
    globalClass = ((GlobalClass)getApplicationContext());

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popupWindowView = inflater.inflate(R.layout.popup_no_connection, null, false);
    Button refreshConnection = (Button) popupWindowView.findViewById(R.id.btn_reconnect);
    refreshConnection.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (globalClass.getErrorHandler().getNoConnectionWithServer() != null) {
                Communication communication = new Communication(globalClass, entrance.this);
                globalClass.setCommunication(communication);
                Thread t = new Thread(communication);
                t.start();
                globalClass.getErrorHandler().getNoConnectionWithServer().dismiss();
            }
        }
    });

最佳答案

当然,不会调用onClick回调。为什么?因为:

// you find button in a view 
View popupWindowView = inflater.inflate(R.layout.popup_no_connection, null, false); // this return new view
Button refreshConnection = (Button) popupWindowView.findViewById(R.id.btn_reconnect);

// but you create pop up with another view not from above (which contain listener)
final PopupWindow noConnection = new PopupWindow(
                    inflater.inflate(R.layout.popup_no_connection, null, false),
                    800,
                    800,
                    true);

那么如何解决呢?有两种方法:
1.在inflate编码后保存您的弹出 View ,并在runnable中创建弹出 View 时重新使用。
2.创建Button后在setOnclickListener中找到您的Runnablepop-up view

关于java - 试图在线程中按下PopupWindow的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36151691/

相关文章:

c - 通过套接字读/写C编程

java - 通过编辑文本过滤结果

java - 尝试传输到 irs a2a 系统时收到 TPE 错误 1105

android - ionic 2 : APK output corrupted on android device

java - 空对象引用上的“android.os.Looper.quit()” - Android O

multithreading - Racket 中的公平线程调度程序时基

java - 使用 java web start 的小程序的替代方法

java - netbeans 7.3 中没有 Web 应用程序选项

java - 当我删除应用程序时,未调用 onReceive 方法,并且我以正确的方式编写代码

c++ - 如何安排这个多线程应用程序?