Custom Layout causing crash if i call something on onVisibilityChanged or onWindowFocusChanged on android
I have a custom RelativeLayo开发者_Go百科ut 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
Fixed: I was setting the RelativeLayout's layoutsParams inside that function.
精彩评论