在API 19上获得ClassCast异常,但在API 21上正常工作

时间:2015-05-25 09:57:44

标签: android eclipse navigation-drawer material-design

我已经实施了material design navigation drawercustom toolbar。它在Lollipop设备上运行得非常好。

但是当Android 4.4.4, it crashes上运行时出现以下错误:

  

java.lang.ClassCastException:android.view.ViewGroup $ LayoutParams   无法强制转换为android.widget.AbsListView $ LayoutParams

我没有改变导航抽屉的任何布局。用于设置抽屉的代码示例:

mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
                .findFragmentById(R.id.fragment_drawer);

        // Set up the drawer.
        mNavigationDrawerFragment.setup(R.id.fragment_drawer,
                (DrawerLayout) findViewById(R.id.drawer), mToolbar);

LogCat:

 java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
            at android.widget.ListView.setupChild(ListView.java:1826)
            at android.widget.ListView.makeAndAddView(ListView.java:1793)
            at android.widget.ListView.fillDown(ListView.java:691)
            at android.widget.ListView.fillFromTop(ListView.java:752)
            at android.widget.ListView.layoutChildren(ListView.java:1630)
            at android.widget.AbsListView.onLayout(AbsListView.java:2087)
            at android.view.View.layout(View.java:14857)
            at android.view.ViewGroup.layout(ViewGroup.java:4643)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14857)
            at android.view.ViewGroup.layout(ViewGroup.java:4643)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14857)
            at android.view.ViewGroup.layout(ViewGroup.java:4643)
            at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:907)
            at android.view.View.layout(View.java:14857)
            at android.view.ViewGroup.layout(ViewGroup.java:4643)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
            at android.view.View.layout(View.java:14857)
            at android.view.ViewGroup.layout(ViewGroup.java:4643)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14857)
            at android.view.ViewGroup.layout(ViewGroup.java:4643)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14857)
            at android.view.ViewGroup.layout(ViewGroup.java:4643)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14857)
            at android.view.ViewGroup.layout(ViewGroup.java:4643)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14857)
            at android.view.ViewGroup.layout(ViewGroup.java:4643)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14857)
            at android.view.ViewGroup.layout(ViewGroup.java:4643)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2013)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1770)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

感谢任何帮助。

3 个答案:

答案 0 :(得分:3)

Android已在AppCompat v21中进行了重大更改。

所以,请参考此链接: http://android-developers.blogspot.in/2015/04/android-support-library-221.html

答案 1 :(得分:2)

我也遇到了同样的问题。我使用了适用于GridView的适配器。当我在getView()方法中设置LinearLayout的LayoutParams时会发生这种错误。我用这段代码解决了这个问题

var Ship = function () {
    return {
        canvas: $('#area')[0],
        ship_image: new Image(),
        ship_width: null,
        ship_height: null,
        ship_x: 0,
        ship_y: 0,
        init: function () {

            this.context = this.canvas.getContext("2d");
            this.canvas_width = this.canvas.width;
            this.canvas_height = this.canvas.height;

            this.ship_image.onload = function () {
                this.ship_width = this.ship_image.width;
                this.ship_height = this.ship_image.height;
                this.ship_x = (this.canvas_width / 2) - (this.ship_width / 2);
                this.ship_y = this.canvas_height - this.ship_image.height;

                this.draw(this.ship_x, this.ship_y);
            }.bind(this);

            this.ship_image.src = "ship.gif";
        },
        draw: function (x, y) {
            this.context.drawImage(this.ship_image, x, y);
        }
    }
}();

而不是

linearLayout.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));

答案 2 :(得分:0)

尝试并使用AppCompatv21。然后可能会出现错误,因为Material Design将与使用此AppCompat的prelollipop版本兼容。

http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html