ClassCastException异常铸造ViewPager时 [英] ClasscastException when casting to ViewPager

查看:198
本文介绍了ClassCastException异常铸造ViewPager时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个基于从保持兼容库中的新ViewPager的应用程序。
当我将行从示例应用程序复制:

I am trying to create an application based on the new ViewPager from the compability library. When I copy the lines from the sample application:

setContentView(R.layout.main);
MyAdapter mAdapter = new MyAdapter(getSupportFragmentManager(), this);
ViewPager mPager = (ViewPager) findViewById(R.id.view_pager);
mPager.setAdapter(mAdapter);

具有以下main.xml中:

with the following main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <android.support.v4.view.ViewPager 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/view_pager"
    />
<LinearLayout>

我收到了

java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout

在这里我转换为ViewPager行。
有没有人有一个想法,为什么发生这种情况?

at the line where I cast to ViewPager. Does anyone have an idea why this is happening?

推荐答案

北京时间其实老布局膨胀问题的问题(外观的此处)。对于一些神奇的原因findViewByID不空回到这里。而是返回null它返回一个android.support.v4.app.NoSaveStateFrameLayout这是非常可笑的BTW。

Actually the problem ist the old layout inflating problem (look here). For some magic reasons findViewByID doesn't return null here. Instead of returning null it returns a android.support.v4.app.NoSaveStateFrameLayout which is very funny btw.

这篇关于ClassCastException异常铸造ViewPager时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆