ClassCastException - 在Honeycomb中的findViewById(android.R.id.content)

时间:2015-09-28 22:19:46

标签: android android-3.0-honeycomb android-compatibility android-settings

我已经成功地使用以下代码来支持材质设计PreferenceActivity,但是今天我从具有ClassCast异常的用户那里获得了一个日志文件。我无法重现它,但怀疑它可能与她的设备在API 13上有关.Android的虚拟设备管理器没有此版本的任何系统映像,所以我无法检查。

我假设,在她的Android版本中,此调用返回的视图是LinearLayout,而不是ListView。

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

这是完整的日志和代码:

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

以下是抛出错误的代码。我很感激任何见解。谢谢!

@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);
        }

0 个答案:

没有答案