0

I have been using the following code rather successfully to support a material design PreferenceActivity, but today I got a log file from a user with a ClassCast exception. I am unable to reproduce it, but suspect it may have to do with her device being on API 13. Android's Virtual Device Manager doesn't have any system images for this version, so I can't check.

I hypothesize that, on her version of Android, the view returned from this call is a LinearLayout, not a ListView.

ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
ListView content = (ListView) root.getChildAt(0); 

Here's the full log and code:

09-28 17:16:26.910 W/System.err( 2938): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app/com.my.app.SettingsActivity}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ListView
09-28 17:16:26.910 W/System.err( 2938):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1818)
09-28 17:16:26.910 W/System.err( 2938):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1834)
09-28 17:16:26.910 W/System.err( 2938):     at android.app.ActivityThread.access$500(ActivityThread.java:122)
09-28 17:16:26.910 W/System.err( 2938):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1027)
09-28 17:16:26.910 W/System.err( 2938):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 17:16:26.910 W/System.err( 2938):     at android.os.Looper.loop(Looper.java:132)
09-28 17:16:26.910 W/System.err( 2938):     at android.app.ActivityThread.main(ActivityThread.java:4126)
09-28 17:16:26.910 W/System.err( 2938):     at java.lang.reflect.Method.invokeNative(Native Method)
09-28 17:16:26.910 W/System.err( 2938):     at java.lang.reflect.Method.invoke(Method.java:491)
09-28 17:16:26.910 W/System.err( 2938):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
09-28 17:16:26.910 W/System.err( 2938):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
09-28 17:16:26.910 W/System.err( 2938):     at dalvik.system.NativeStart.main(Native Method)
09-28 17:16:26.910 W/System.err( 2938): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ListView
09-28 17:16:26.920 W/System.err( 2938):     at com.my.app.SettingsActivity.onPostCreate(SourceFile:168)
09-28 17:16:26.920 W/System.err( 2938):     at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1113)
09-28 17:16:26.920 W/System.err( 2938):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1801)
09-28 17:16:26.920 W/System.err( 2938):     ... 11 more

Below is the code that throws the error. I would appreciate any insight. Thank you!

@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        Toolbar bar;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            LinearLayout root = (LinearLayout) findViewById(android.R.id.list).getParent().getParent().getParent();
            bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.app_bar, root, false);
            root.addView(bar, 0); // insert at top
        } else {
            ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
            ListView content = (ListView) root.getChildAt(0);  // ERROR HERE

            root.removeAllViews();

            bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.app_bar, root, false);


            int height;
            TypedValue tv = new TypedValue();
            if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
                height = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
            } else {
                height = bar.getHeight();
            }

            content.setPadding(0, height, 0, 0);

            root.addView(content);
            root.addView(bar);
        }
NSouth
  • 5,067
  • 7
  • 48
  • 83

0 Answers0