我想在MainActivity中去监听另一个layout中的edittext 在MainActivity中有用到ViewPage 但是在oncreate中初始化 然后去监听 会出空指针的错
但是如果在edittext所在的layout中加一个button 然后给 一个android:onClick="onclick"
然后在MainActivity中去实现这个点击事件 在这个事件下去初始化edittext实现监听就不会出错 但是这样会导致edittext.addTextChangedListener 多次的使用 如果edittext很多的话会导致程序崩溃
有什么办法能在点击事件外去监听这些edittext 不出NullpointerException的错误啊
Your question is very confusing. Only certain components in the Activity can be monitored. Whether it is a pop-up dialog box or a component in a Fragment, it must be based on the Activity, so it has nothing to do with other layouts. .
The components in Activity all rely on findViewById(); for initialization. If it is in other layouts, the layout file will be converted into View, and then initialized through findViewById. This means that you must know which View your editText is under and which one it is under. Inside Activity.
By the way, have I answered the previous questions? - -
There must be an object first before there can be operations on the object. Your editText is in the viewpager, so implement monitoring of the editText in the viewpager's adapter.
First of all, why do you want to monitor things in another activity? If you want to get the content and change it, you can pass it through handler or intent
Write a callback yourself, or use eventbus. Please go to github for details on how to use it