Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException when retrieving custom view

I have a layout like this:

<com.mypackage.QuadPaneHorizontalSplit
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<!-- more views here -->

</com.mypackage.QuadPaneHorizontalSplit>

The class QuadPaneHorizontalSplit extends QuadPaneLayout (which extends LineraLayout):

public class QuadPaneHorizontalSplit extends QuadPaneLayout {

    public QuadPaneHorizontalSplit(Context context) {
        super(context);
    }

    public QuadPaneHorizontalSplit(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public QuadPaneHorizontalSplit(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected boolean isVerticalSplit() {
        return false;
    }

}

After settings the layout with setContentView I try to retrieve the custom view:

QuadPaneLayout quadPaneLayout = (QuadPaneLayout) findViewById(R.id.root_layout);

which works great in general but I got some crash reports with the following exception:

java.lang.ClassCastException: com.mypackage.QuadPaneHorizontalSplit cannot be cast to com.mypackage.QuadPaneLayout

QuadPaneHorizontalSplit clearly extends QuadPaneLayout so the exception should not be possible. I get crash reports from Android 4.x devices and different manufacturers. Interesting is that all the devices are rooted so maybe this is an issue with some custom rom? Note that this line of code is executed thousands of times each day without issues and so far I got only 13 crashes (BugSense) but I'd still like to get to the bottom of this.

I know about this: https://stackoverflow.com/a/7823787/534471 but I'm not using include for this layout and I wouldn't want to add a merge tag because that might have undesired effects.

Anyone has an idea what's going on here?

like image 929
Emanuel Moecklin Avatar asked Feb 17 '14 15:02

Emanuel Moecklin


1 Answers

I finally found the answer to this question which might be of interest to other developers too.

Since I had lots of other inexplicable crashes including VerifyErrors, ClassNotFoundExceptions and ActivityNotFoundExceptions I got suspicious about the origin of these crash reports. I added some "extra code" to make sure the app wasn't tampered with and if it was the bug report would show this. Indeed all the ClassCastException crashes happened in instances of my app that were obviously cracked which would also explain why I got this crash reports only for the paid version of the app and why the paid app seems to crash much more often than the free one.

Maybe it's time to spend that extra money for a DexGuard license...

like image 164
Emanuel Moecklin Avatar answered Sep 27 '22 23:09

Emanuel Moecklin