Android中的类强制转换异常

时间:2014-11-10 18:57:07

标签: android listview classcastexception android-relativelayout

我正在使用导航抽屉制作一个Android应用程序。我还想在用户第一次打开应用程序时显示透明的指令屏幕。为此,我在我的xml文件中有一个相对的布局,以及一个抽屉的listView。

XML文件:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.twistedlines.overbudget.MainActivity"
    tools:ignore="MergeRootFrame" >

    <!-- transparent overlay -->
    <RelativeLayout
        android:id="@+id/top_transparent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#77CCCCCC" >

        <ImageView
            android:id="@+id/transparent_img"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/contdesc"
            android:scaleType="fitXY"
            android:src="@drawable/instructions" />
    </RelativeLayout>

</FrameLayout>

<ListView android:id="@+id/drawerList"
    android:layout_height="match_parent"
    android:layout_width="240dp" 
    android:layout_gravity="start"
    android:divider="#c7c7c7"
    android:dividerHeight="3dp"
    android:background="#bdbdbd"></ListView>

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

但是,我在这行中的java文件中得到了classCastException:

drawerList = (ListView) findViewById(R.id.drawerList);

logcat的:

  

11-11 00:10:53.417:E / test(8163):例外11-11 00:10:53.437:   E / AndroidRuntime(8163):致命例外:主11-11 00:10:53.437:   E / AndroidRuntime(8163):java.lang.RuntimeException:无法启动   活动   ComponentInfo {com.twistedlines.overbudget / com.twistedlines.overbudget.MainActivity}:   java.lang.ClassCastException:android.widget.RelativeLayout不能   强制转换为android.widget.ListView 11-11 00:10:53.437:   E / AndroidRuntime(8163):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2062)   11-11 00:10:53.437:E / AndroidRuntime(8163):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2087)   11-11 00:10:53.437:E / AndroidRuntime(8163):at   android.app.ActivityThread.access $ 600(ActivityThread.java:133)11-11   00:10:53.437:E / AndroidRuntime(8163):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1198)   11-11 00:10:53.437:E / AndroidRuntime(8163):at   android.os.Handler.dispatchMessage(Handler.java:99)11-11   00:10:53.437:E / AndroidRuntime(8163):at   android.os.Looper.loop(Looper.java:137)11-11 00:10:53.437:   E / AndroidRuntime(8163):at   android.app.ActivityThread.main(ActivityThread.java:4792)11-11   00:10:53.437:E / AndroidRuntime(8163):at   java.lang.reflect.Method.invokeNative(Native Method)11-11   00:10:53.437:E / AndroidRuntime(8163):at   java.lang.reflect.Method.invoke(Method.java:511)11-11 00:10:53.437:   E / AndroidRuntime(8163):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:808)   11-11 00:10:53.437:E / AndroidRuntime(8163):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)11-11   00:10:53.437:E / AndroidRuntime(8163):at   dalvik.system.NativeStart.main(Native Method)11-11 00:10:53.437:   E / AndroidRuntime(8163):引起:java.lang.ClassCastException:   android.widget.RelativeLayout无法强制转换为   android.widget.ListView 11-11 00:10:53.437:E / AndroidRuntime(8163):     在   com.twistedlines.overbudget.MainActivity.onCreate(MainActivity.java:118)   11-11 00:10:53.437:E / AndroidRuntime(8163):at   android.app.Activity.performCreate(Activity.java:5008)11-11   00:10:53.437:E / AndroidRuntime(8163):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)   11-11 00:10:53.437:E / AndroidRuntime(8163):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2026)

它说RelativeLayout不能投射到ListView。但在我的XML中,drawerList是ListView,而不是RelativeLayout。有什么问题?

我的drawerList变量是一个ListView对象。这是宣言:

DrawerLayout drawerLayout;
    ListView drawerList;
    ActionBarDrawerToggle abdToggle;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        topLevelLayout = findViewById(R.id.top_transparent);



           if (isFirstTime()) {
                topLevelLayout.setVisibility(View.INVISIBLE);
            }
        drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
        drawerList = (ListView) findViewById(R.id.drawerList);   //exception line
        adaptr=new mAdapter(this);
        drawerList.setAdapter(adaptr);
        drawerList.setOnItemClickListener(new itemListener());
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);
        getSupportActionBar().setTitle("Overview");

       //rest of the code

1 个答案:

答案 0 :(得分:0)

你的代码对我来说似乎没问题。试着再次清理并重建它。

清洁。 转到项目 - &gt;清洁。选择您的项目,然后单击确定

要重新重建它,只需运行您的代码。