Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams without LayoutParams usage

Tags:

android

I use a view which has RelativeLayout -> ScrollView -> LinearLayout. I get exception:

11-22 11:17:24.605: ERROR/AndroidRuntime(5161): FATAL EXCEPTION: main
11-22 11:17:24.605: ERROR/AndroidRuntime(5161): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
11-22 11:17:24.605: ERROR/AndroidRuntime(5161):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3131)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161):     at android.view.View.measure(View.java:8172)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161):     at android.view.ViewRoot.performTraversals(ViewRoot.java:805)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161):     at android.os.Looper.loop(Looper.java:144)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161):     at android.app.ActivityThread.main(ActivityThread.java:4937)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161):     at java.lang.reflect.Method.invokeNative(Native Method)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161):     at java.lang.reflect.Method.invoke(Method.java:521)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161):     at dalvik.system.NativeStart.main(Native Method)

Why does it happen only occassionally? Why is there no detail of where the code failed?

Update: I suspect it should fail around this code:

EditText input = new EditText(context);
input.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

LayoutParams is RelativeLayout.LayoutParams

like image 834
Taranfx Avatar asked Nov 22 '11 12:11

Taranfx


People also ask

What is android ViewGroup?

ViewGroup is a collection of Views(TextView, EditText, ListView, etc..), somewhat like a container. A View object is a component of the user interface (UI) like a button or a text box, and it's also called a widget.

What is child view in android?

In android (and most other technologies), views can have subviews, aka "children". Most views can have children and can be a child of a parent view. It's kind of like a "tree". The most obvious feature of being a child of some other view is that the child moves with the parent view.

What is view and view group?

View is a basic building block of UI (User Interface) in android. A view is a small rectangular box which responds to user inputs. Eg: EditText , Button , CheckBox , etc.. ViewGroup is a invisible container of other views (child views) and other viewgroups.

Which of the following is a view group?

Android supports the following ViewGroups: LinearLayout. AbsoluteLayout. TableLayout.


1 Answers

Use new LinearLayout.LayoutParams(...) instead of new LayoutParams(...)

You always need to use the parent layout's class for the LayoutParams. E.g. if EditText is inside FrameLayout then FrameLayout.LayoutParams must be used.

like image 142
Jarno Argillander Avatar answered Oct 19 '22 06:10

Jarno Argillander