如何调试此ClassCastException? (堆栈跟踪似乎无用)

时间:2013-09-20 20:45:26

标签: android android-layout

下面的堆栈跟踪对我来说似乎毫无用处......有没有人知道如何找出导致崩溃的原因?

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)

是否有一种技术可以用来解决这个问题?

3 个答案:

答案 0 :(得分:3)

这不是没用的。它告诉您,您正在某处创建RelativeLayout.LayoutParams的实例并在使用AbsListView.LayoutParams的地方使用它。当您以编程方式设置LayoutParams时,通常会发生这种情况。

答案 1 :(得分:1)

使用LayoutParams,它始终发生。每个版本有各种不同的版本,您需要确保导入适合您尝试使用的版本。

当您应该导入android.widget.AbsListView.LayoutParams时,您正在导入android.widget.RelativeLayout.LayoutParams。反之亦然。

答案 2 :(得分:1)

Android为其视图使用多个LayoutParam类,具有类似的命名约定。这里要注意的关键是它们都是它们所用布局的静态嵌套类,并扩展了基类ViewGroup.LayoutParams类。

例如,您有:

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

等等。您遇到的问题是您使用了错误的LayoutParams,这就是为什么它会给您ClassCastException。确保您使用适当的LayoutParams作为您正在使用的布局。

在您的情况下,您使用RelativeLayout.LayoutParams代替AbsListView.LayoutParams