ClassCastException on support-library-v7 , using v

2019-06-21 16:33发布

问题:

Background

Over the past 2 weeks I've been moving my app from ActionBarSherlock to the official support library of Google, to also gain Material-Design style.

I've updated to the newest one (which is 21), and I've included both v4 and v7 of it.

My app has a simple navigation-drawer , an action-bar, and a fragment container to switch between the fragments (which I only have 2). The rest of the screens are the settings screen and shortcuts to OS's tools. I also have a widget-configuration screen, but that's irrelevant .

The problem

I got a crash from a user that claims it crashes every time, on LG-G2, with Android 4.4 :

java.lang.ClassCastException: android.support.v7.internal.widget.m cannot be cast to com.android.internal.widget.ActionBarOverlayLayout$LayoutParams
at com.android.internal.widget.ActionBarOverlayLayout.applyInsets(ActionBarOverlayLayout.java:172)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:317)
at android.view.View.measure(View.java:16538)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2374)
at android.view.View.measure(View.java:16538)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1964)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1160)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1342)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1047)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5896)
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:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)

The XML I use

Here's a short version of the XML I use for the layout:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:id="@+id/activity_app_list__drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <FrameLayout
                android:id="@+id/activity_app_list__fragmentContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@+id/activity_app_list__adContainer"
                android:layout_alignParentTop="true" />

            <FrameLayout
                android:id="@+id/activity_app_list__adContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true" />
        </RelativeLayout>

        <ScrollView
            android:id="@+id/activity_app_list__drawerView"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="?android:attr/windowBackground" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <==just some views for the drawer...
            </LinearLayout>
        </ScrollView>

    </android.support.v4.widget.DrawerLayout>

What I've found

I've searched the Internet, and found almost nothing about this problem.

The only thing I've found is this post, but they don't tell how and why it happens. Plus I don't even use this library, and I'm not sure it's the same error. The tip there was to remove the next code:

(ViewGroup)findViewById(android.R.id.content).setLayoutParams(new LinearLayout.LayoutParams(-1, -1));

The question

Why does it occur? How can I fix it?

Will using the tip on the post help?

回答1:

I had the same problem and found the culprit. It's when you have these 2 elements in your AppCompat theme:

<item name="android:windowNoTitle">false</item>
<item name="android:windowActionBar">true</item>

The crash only seems to occur on KitKat.