Android中的类强制转换异常
我正在制作一个带有导航抽屉的android应用程序。我还想在用户第一次打开应用程序时显示一个透明的指示屏幕。为此,我在xml文件中有一个相对布局,抽屉有一个listView XML文件:Android中的类强制转换异常,android,listview,classcastexception,android-relativelayout,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
<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>
日志:
11-11 00:10:53.417:E/test8163:Exception 11-11 00:10:53.437:
E/AndroidRuntime8163:致命异常:main 11-11 00:10:53.437:
E/AndroidRuntime8163: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/AndroidRuntime8163:at
android.app.ActivityThread.performLaunchActivityActivityThread.java:2062
11-11 00:10:53.437:E/AndroidRuntime8163:at
android.app.ActivityThread.handleLaunchActivityActivityThread.java:2087
11-11 00:10:53.437:E/AndroidRuntime8163:at
android.app.ActivityThread.access$600ActivityThread.java:133 11-11
00:10:53.437:E/AndroidRuntime8163:at
android.app.ActivityThread$H.handleMessageActivityThread.java:1198
11-11 00:10:53.437:E/AndroidRuntime8163:at
android.os.Handler.dispatchMessageHandler.java:99 11-11
00:10:53.437:E/AndroidRuntime8163:at
android.os.Looper.Looper.java:137 11-11 00:10:53.437:
E/AndroidRuntime8163:at
android.app.ActivityThread.mainActivityThread.java:4792 11-11
00:10:53.437:E/AndroidRuntime8163:at
java.lang.reflect.Method.Invokenative方法11-11
00:10:53.437:E/AndroidRuntime8163:at
java.lang.reflect.Method.invokeMethod.java:5111-11 00:10:53.437:
E/AndroidRuntime8163:at
java:808
11-11 00:10:53.437:E/AndroidRuntime8163:at
java:575 11-11
00:10:53.437:E/AndroidRuntime8163:at
dalvik.system.NativeStart.Main本地方法11-11 00:10:53.437:
E/AndroidRuntime8163:原因:java.lang.ClassCastException:
无法将android.widget.RelativeLayout强制转换为
android.widget.ListView 11-11 00:10:53.437:E/AndroidRuntime8163:
在
com.twistedlines.overbudget.MainActivity.onCreateMainActivity.java:118
11-11 00:10:53.437:E/AndroidRuntime8163:at
android.app.Activity.performCreateActivity.java:5008 11-11
00:10:53.437:E/AndroidRuntime8163:at
android.app.Instrumentation.callActivityOnCreateInstrumentation.java:1079
11-11 00:10:53.437:E/AndroidRuntime8163:at
java:2026
它说RelativeLayout不能在ListView中强制转换。但在我的XML中,drawerList是一个ListView,而不是RelativeLayout。有什么问题
我的抽屉列表变量是一个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
我觉得你的代码没问题。只要试着重新清理和重建它 用于清洁。 进入项目->清洁。选择您的项目并单击“确定”
要重新生成,只需运行代码。抽屉列表首先定义在哪里?如果100%确定引用的ID正确,请清理并重新生成。您没有多个布局文件夹,是吗?如果您这样做,您可能会将该id用作应用程序正在选择的文件夹中的相对布局。我会快速按住ctrl+shift+f键,查看抽屉列表是否在另一个布局文件中。清理项目成功了。非常感谢。你能加上这个作为回答,这样我就可以接受了吗?
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