简体   繁体   English

侦听器将无法正常工作

[英]Listener won't work

I'm trying to set a Listener for my custom views that I put in a LinearLayout. 我正在尝试为放置在LinearLayout中的自定义视图设置侦听器。 Those views are created by code depending on a certain files or data, so I don't know the number of views there is. 这些视图是由代码根据某些文件或数据创建的,所以我不知道那里有多少视图。 The OnClickListener is being set, but onClick() is never called: 正在设置OnClickListener,但是从不调用onClick():

@Override
    protected void onResume() {
        super.onResume();
        try {
            setupBackEnd();
        } catch (IOException e) {
            Toast.makeText(this, "Error Reading or Writing data in Storage, Try to restart the App", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

        Toast.makeText(DayActivity.this, "Child count: " + linearList.getChildCount(), Toast.LENGTH_SHORT).show();
        for (int i = 0; i < linearList.getChildCount(); i++) {
            linearList.getChildAt(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(DayActivity.this, "clicked", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

This is my code. 这是我的代码。 To make sure the child views are already there I showed a Toast and it gave me "Child Count: 5" but "clicked" Toast never shows up. 为了确保子视图已经存在,我展示了一个Toast,它给了我“ Child Count:5”,但是“单击” Toast从未显示。 I made sure that My custom Views are clickable (The childs of linearlist). 我确保我的自定义视图是可单击的(linearlist的子代)。 But the listener won't work for some reason. 但是由于某种原因,监听器将无法工作。 Anyone knows why? 有人知道为什么吗?

Edit: After I tried to debug the code I got this error: 编辑:尝试调试代码后,出现此错误:

10-27 18:52:26.309 17630-17630/osm_cave.timecave E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: osm_cave.timecave, PID: 17630
                                                                   android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@672721e is not valid; is your activity running?
                                                                       at android.view.ViewRootImpl.setView(ViewRootImpl.java:679)
                                                                       at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
                                                                       at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
                                                                       at android.widget.Toast$TN.handleShow(Toast.java:459)
                                                                       at android.widget.Toast$TN$2.handleMessage(Toast.java:342)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
c

This error won't be shown when I launch the app without debugging it only appears when Debugging with breakpoints. 如果我在未调试的情况下启动应用程序,则不会显示此错误,只会在使用断点进行调试时显示。 This error is supposed to mean I'm trying to use a context that no longer exist but I'm not so what's the problem here?? 这个错误应该意味着我正在尝试使用一个不再存在的上下文,但是我不是,这里的问题是什么?

It looks like your toast message is running into an error. 您的祝酒讯息似乎出现错误。

 at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
 at android.widget.Toast$TN.handleShow(Toast.java:459)
 at android.widget.Toast$TN$2.handleMessage(Toast.java:342)
 at android.os.Handler.dispatchMessage(Handler.java:102)

If I remember correctly, the new object that you are creating of type View.OnClickListener (since it is anonymous) cannot access the DayActivity.this attribute. 如果我没记错的话,您正在创建的View.OnClickListener类型的新对象(由于它是匿名的)无法访问DayActivity.this属性。 Instead you should get the activity from the view. 相反,您应该从视图中获取活动。

public void onClick(View view) {
    Activity activity = (Activity) view.getContext()
    Toast.makeText(activity, "clicked", Toast.LENGTH_SHORT).show();
}

While there's nothing wrong with using a Toast message to test for an event occurring, I would recommend simplifying to just using a log message. 尽管使用Toast消息测试事件的发生并没有错,但我建议您简化为仅使用日志消息。 Log messages aren't context sensitive are unlikely to fail except if you provide a null message to it. 日志消息不是上下文敏感的,除非您提供空消息,否则它不太可能失败。

If Log messages are not working, the next troubleshooting step should be to examine the custom views and view hierarchy. 如果日志消息不起作用,则下一步故障排除步骤应该是检查自定义视图和视图层次结构。 If any clickable transparent views are in front of the custom views, they would intercept the click events. 如果自定义视图前面有任何可单击的透明视图,它们将拦截单击事件。 Double check that the view itself is clickable as well. 再次检查视图本身是否也是可单击的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM