如果我在 android 上的 onVisibilityChanged 或 onWindowFocusChanged 上调用某些内容,自定义布局会导致崩溃
我有一个自定义的RelativeLayout,里面有一个创建一些迷你视图的函数。 如果我在构造函数中调用该函数就可以了。但我想在调用该函数之前知道布局的尺寸。 因此,如果我在 onVisibilityChanged
onWindowFocusChanged
中调用该函数,我就会获得布局的尺寸,因为它已填充。问题是,如果我在那里这样做,我会得到例外。 另外,如果我像这样延迟构造函数中的函数
handler.postDelayed(new Runnable(){
public void run(){
AnONcamelCaseFUNction();
}
}, 2000);
,我会得到相同的错误(该错误是无关的堆栈跟踪不会有帮助。)我认为这与“如果已经填充了数据视图,请不要弄乱它......”但我不知道该怎么办
更新: 这是我添加视图的方法。 (许多线性布局到一个大的相对布局)。请注意,它仅不适用于 post.executions。在构造函数中它可以工作。
LinearLayout cells = null;
RelativeLayout.LayoutParams tr;
tr = new RelativeLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
cells = new LinearLayout(context);
cells.setLayoutParams(tr);
addView(cells);
堆栈跟踪:
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.widget.FrameLayout.onLayout(FrameLayout.java:293)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.view.View.layout(View.java:7278)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.view.View.layout(View.java:7278)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.view.View.layout(View.java:7278)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.view.ViewRoot.performTraversals(ViewRoot.java:1172)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.view.ViewRoot.handleMessage(ViewRoot.java:1921)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.os.Looper.loop(Looper.java:143)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.app.ActivityThread.main(ActivityThread.java:4196)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at java.lang.reflect.Method.invoke(Method.java:507)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at dalvik.system.NativeStart.main(Native Method)
enter code here
I have a custom RelativeLayout and inside it i have a function that create some mini views.
If i call that function in the constructor it's ok. But i want to know the layout's dimentions before i call that function.
So if i call the function in onVisibilityChanged
onWindowFocusChanged
i have the layout's dimensions since it's populated. The problem is that i get exception if i do it there.
Also if i delay the function in the constructor like that
handler.postDelayed(new Runnable(){
public void run(){
AnONcamelCaseFUNction();
}
}, 2000);
i get the same error ( The error is irrelevant stacktrace won't help.) I think it has to do with "don't mess with da view if it's already populated... but i don't know what to do.
UPDATE:
Here is how i add the views. (Many linearLayouts to one big RelativeLayout). Plz notice that it only don't work on post.executions. In the constructor it works.
LinearLayout cells = null;
RelativeLayout.LayoutParams tr;
tr = new RelativeLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
cells = new LinearLayout(context);
cells.setLayoutParams(tr);
addView(cells);
Stacktrace:
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.widget.FrameLayout.onLayout(FrameLayout.java:293)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.view.View.layout(View.java:7278)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.view.View.layout(View.java:7278)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.view.View.layout(View.java:7278)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.view.ViewRoot.performTraversals(ViewRoot.java:1172)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.view.ViewRoot.handleMessage(ViewRoot.java:1921)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.os.Looper.loop(Looper.java:143)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at android.app.ActivityThread.main(ActivityThread.java:4196)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at java.lang.reflect.Method.invoke(Method.java:507)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-04 02:29:22.748: ERROR/AndroidRuntime(11355): at dalvik.system.NativeStart.main(Native Method)
enter code here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已修复:我在该函数内设置relativelayout的layoutsParams。
Fixed: I was setting the RelativeLayout's layoutsParams inside that function.