0

The stack trace below seems useless to me... does anyone have an idea how to find out what caused the crash?

09-20 13:40:11.136: D/AndroidRuntime(8819): Shutting down VM
09-20 13:40:11.136: W/dalvikvm(8819): threadid=1: thread exiting with uncaught exception (group=0x410b82a0)
09-20 13:40:11.146: E/AndroidRuntime(8819): FATAL EXCEPTION: main
09-20 13:40:11.146: E/AndroidRuntime(8819): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.ListView.setupChild(ListView.java:1809)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.ListView.makeAndAddView(ListView.java:1778)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.ListView.fillDown(ListView.java:678)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.ListView.fillFromTop(ListView.java:739)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.ListView.layoutChildren(ListView.java:1628)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.AbsListView.onLayout(AbsListView.java:2303)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.View.layout(View.java:14063)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.ViewGroup.layout(ViewGroup.java:4655)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.View.layout(View.java:14063)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.ViewGroup.layout(ViewGroup.java:4655)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.View.layout(View.java:14063)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.ViewGroup.layout(ViewGroup.java:4655)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.View.layout(View.java:14063)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.ViewGroup.layout(ViewGroup.java:4655)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.View.layout(View.java:14063)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.ViewGroup.layout(ViewGroup.java:4655)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.View.layout(View.java:14063)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.ViewGroup.layout(ViewGroup.java:4655)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.View.layout(View.java:14063)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.ViewGroup.layout(ViewGroup.java:4655)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.View.layout(View.java:14063)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.ViewGroup.layout(ViewGroup.java:4655)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.View.layout(View.java:14063)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.ViewGroup.layout(ViewGroup.java:4655)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.View.layout(View.java:14063)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.ViewGroup.layout(ViewGroup.java:4655)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.View.layout(View.java:14063)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.ViewGroup.layout(ViewGroup.java:4655)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.View.layout(View.java:14063)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.ViewGroup.layout(ViewGroup.java:4655)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2017)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1838)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1131)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4611)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.Choreographer.doFrame(Choreographer.java:525)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.os.Handler.handleCallback(Handler.java:615)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.os.Looper.loop(Looper.java:137)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at android.app.ActivityThread.main(ActivityThread.java:4898)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at java.lang.reflect.Method.invokeNative(Native Method)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at java.lang.reflect.Method.invoke(Method.java:511)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
09-20 13:40:11.146: E/AndroidRuntime(8819):     at dalvik.system.NativeStart.main(Native Method)

Is there a technique to use to solve this ?

Someone Somewhere
  • 23,475
  • 11
  • 118
  • 166

3 Answers3

3

It's not useless. it tells you that you are creating an instance of RelativeLayout.LayoutParams somewhere and use it where you should use a AbsListView.LayoutParams. That usually happens when you set the LayoutParams programatically.

SimonSays
  • 10,867
  • 7
  • 44
  • 59
  • The LayoutParams probably are set programmatically (it's a very large codebase) but the challenge is *where* in the code is this happening because the stack trace doesn't indicate a single line of my code. – Someone Somewhere Sep 20 '13 at 21:17
  • Well i guess you have to click around in your app and see where it crashes. Then you search for the instantiation of `LayoutParameters` in this `Activity`/`Fragment`. Or you can check what layouts have changed lately in case this is an error that did not happen before. – SimonSays Sep 20 '13 at 21:51
  • Solution explanation: a Listview was adding a dynamically-created item . To accomplish this, one *must* use an `AbsListView.LayoutParams`, even a `ViewGroup.LayoutParams` won't work because it'll except later when the ListView draws itself (the `setLayoutParams()` didn't show in the stack because it was drawing the listview and so wasn't in my code). The difficulty in debugging this was due to the *very many* `setLayoutParams()` calls in the code - a little over 50 of them, add to that the delayed exception that happened only after listview drew itself. – Someone Somewhere Sep 23 '13 at 20:48
  • I have been dealing with this error for the past half hour and was getting very frustrated because I know my layouts were correct BUT somehow I missed the "LayoutParams" part of the error LOL java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams – dawoodman71 Nov 10 '16 at 21:14
1

With LayoutParams it happens all the time. There are various different versions of each one and you need to be sure that you are importing the one appropriate to what you are trying to use.

You are importing android.widget.RelativeLayout.LayoutParams when you should be importing android.widget.AbsListView.LayoutParams instead. Or vice-versa.

Jay Snayder
  • 4,298
  • 4
  • 27
  • 53
1

Android uses multiple LayoutParam classes for its views with a similar naming convention. The key thing to note here is that they are all static nested classes of the layout they are for and extend the base ViewGroup.LayoutParams class.

For example, you have:

  • AbsListView.LayoutParams extends ViewGroup.LayoutParams
  • ViewPager.LayoutParams extends ViewGroup.LayoutParams
  • LinearLayout.LayoutParams extends ViewGroup.MarginLayoutParams extends ViewGroup.LayoutParams

and so on. The problem you are having is that you are using the wrong LayoutParams which is why it is giving you a ClassCastException. Make sure that you are using the appropriate LayoutParams for the layout you are using.

In your case, you are using RelativeLayout.LayoutParams instead of AbsListView.LayoutParams.

telkins
  • 10,440
  • 8
  • 52
  • 79
  • I found where in the code the `setLayoutParams()` was called, and I thought I'd be clever and instead of a `RelativeLayout.LayoutParams`, I instantiated a `ViewGroup.LayoutParams`. Unfortunately... `09-20 15:55:33.301: E/AndroidRuntime(18771): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams 09-20 15:55:33.301: E/AndroidRuntime(18771): at android.widget.ListView.setupChild(ListView.java:1809) 09-20 15:55:33.301: E/AndroidRuntime(18771): at android.widget.ListView.makeAndAddView(ListView.java:1778) – Someone Somewhere Sep 20 '13 at 23:01
  • @SomeoneSomewhere Looks like you just got unlucky: http://stackoverflow.com/questions/380813/downcasting-in-java. Downcasting is not always guaranteed to work. The compiler doesn't mark it wrong since it cannot be sure so it puts the trust in you that it's correct. Then at runtime it sees that it cannot convert the `ViewGroup.LayoutParams` into a `RelativeLayout.LayoutParams`. – telkins Sep 21 '13 at 04:48