Android 向导航抽屉添加线性布局(最终导致ClassCastException崩溃)
我正在尝试向导航抽屉添加垂直线性布局(在底部有标题、图像、菜单标题、列表视图和图标)。然而,应用程序崩溃了Android 向导航抽屉添加线性布局(最终导致ClassCastException崩溃),android,navigation-drawer,Android,Navigation Drawer,我正在尝试向导航抽屉添加垂直线性布局(在底部有标题、图像、菜单标题、列表视图和图标)。然而,应用程序崩溃了 'java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams' 错误。我不确定这是否是可能的直道,因为我检查了两个链接,唯一接近我得到的是任何帮助是感谢。 提前谢谢。
'java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams'
错误。我不确定这是否是可能的直道,因为我检查了两个链接,唯一接近我得到的是任何帮助是感谢。
提前谢谢。
这是我的密码。
现在的问题是,xml不允许我定义listview的宽度和高度(元素未知),我也无法运行它,因为编译器说“必须提供布局宽度”属性??请帮忙
我的代码与滚动视图
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main context view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ScrollView
android:id="@+id/leftRL"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginBottom="5dp"
android:background="#fff" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="3dp"
android:src="@drawable/ic_launcher"
android:text="Dummy"
android:textColor="#2E3192"
android:textStyle="bold" />
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#2E3192" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="2dp" >
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#e8e8e8"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dummy name"
android:textSize="15sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dummyemail@imhot.com"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingBottom="3dp"
android:src="@drawable/ic_launcher"
android:text="Dummy item"
android:textColor="#2E3192"
android:textStyle="bold" />
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#2E3192" />
</LinearLayout>
<ListView/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:padding="10dp"
android:src="@drawable/footer_image" />
</LinearLayout>
</ScrollView>
</android.support.v4.widget.DrawerLayout>
做两件事:
gravity=“START”
设置为linearlayout而不是listview,因为linearlayout现在是抽屉此错误是由于导入错误造成的。为布局参数添加适当的导入
此导入“android.widget.LinearLayout.LayoutParams”与LayoutParams ie“android.support.v4.widget.Drawerlayout$LayoutParams”的Drawerlayout库声明混合在一起,导致类强制转换执行。没有代码和堆栈跟踪,您的查询很难理解,但无论如何 请记住,android.support.v4.widget.DrawerLayout只能有3个元素(按此顺序排列):
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com /apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Your main screen -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/topRL"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#DE6D53"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/left_drawer_button"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:onClick="onOpenLeftDrawer"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
<FrameLayout
android:id="@+id/frame_to_be_replaced"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/topRL" />
</RelativeLayout>
<!-- left drawer -->
<RelativeLayout
android:id="@+id/whatYouWantInLeftDrawer"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/black" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title"
android:textColor="@android:color/white" />
<ListView
android:id="@+id/left_expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start" />
<TextView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentBottom="true"
android:textColor="@android:color/white"
android:text="bottom" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
我用你的答案得到了答案,谢谢:)。实际上,问题在于列表视图。我在左边的抽屉里得到了线性布局,但是它没有正确显示,因为我在线性布局中添加了一个滚动视图,我在同一视图中也有一个列表,它只显示列表中的第一项,而整个屏幕空白。我检查了几个链接以获得答案,但它不起作用。最后,由于列表数据对我来说是已知的,并且不会动态更改,我添加了单选按钮,对其进行了自定义并替换了列表 我的代码如下所示
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/app_background" >
<!-- The main context view -->
<FrameLayout
android:id="@+id/fragment_holder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ScrollView
android:id="@+id/leftDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:paddingBottom="3dp"
android:src="@drawable/ic_launcher"
android:text="@string/menu_account"
android:textColor="@color/nav_headingsColor"
android:textStyle="bold" />
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/nav_headingsColor" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgProfilePic"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="3"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgProfilePic"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="3"
android:clickable="true"
android:contentDescription="@null"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/lblName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Christian Bale"
android:textColor="@color/nav_textColor"
android:textSize="18sp" />
<TextView
android:id="@+id/lblEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="christianbale@imhot.com"
android:textColor="@color/nav_textColor"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:paddingBottom="3dp"
android:src="@drawable/ic_launcher"
android:text="@string/menu_menu"
android:textColor="@color/nav_headingsColor"
android:textStyle="bold" />
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/nav_headingsColor" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:layout_marginTop="2dip" >
<RadioButton
android:id="@+id/radioTopTen"
style="@style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:paddingLeft="20dp"
android:text="@string/menu_topTen" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="@color/nav_itemSep" />
<RadioButton
android:id="@+id/radioWish"
style="@style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:paddingLeft="20dp"
android:text="@string/menu_wish" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="@color/nav_itemSep" />
<RadioButton
android:id="@+id/radioGifts"
style="@style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:paddingLeft="20dp"
android:text="@string/menu_gifts" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="@color/nav_itemSep" />
<RadioButton
android:id="@+id/radioLibrary"
style="@style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:paddingLeft="20dp"
android:text="@string/menu_library" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="@color/nav_itemSep" />
<RadioButton
android:id="@+id/radioWhatsOn"
style="@style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:paddingLeft="20dp"
android:text="@string/menu_whatsOn" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="@color/nav_itemSep" />
<RadioButton
android:id="@+id/radioDownload"
style="@style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:paddingLeft="20dp"
android:text="@string/menu_download" />
</RadioGroup>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:padding="10dp"
android:src="@drawable/footer_image" />
</LinearLayout>
</ScrollView>
我知道这可能不是最好的选择,但至少对我来说是可行的。如果有更好的解决方案,请告诉我。尝试一下
在函数selectItem中,
更改mDrawerLayout.closeDrawer(mDrawerList);到
mDrawerLayout.closeDrawer(mDrawerLinear)强>
应该很好用
因为,引用现在应该是您的linearLayout,它不再是您要关闭或打开的listview。很抱歉,代码部分缺少了它。我会把它记下来,以便将来提问。:)谢谢你的回答,我会试试,然后告诉你它是否有效。我想我的代码中唯一缺少的是openDrawer()方法,我在其中传递参数视图本身。您能否告诉我,是否可以创建一个单独的抽屉xml,并在抽屉布局中使用?我没有尝试,但我会避免这种情况,因为DrawerLayout小部件遵循一个页面中最多有3个元素的非常严格的规则…PS:左上方的抽屉视图可以用任何东西和任何东西膨胀…除此之外,我无法想象任何场景。我指的是线性布局。然而,我仍然关心布局本身,我可以忽略部分。请在我的问题中查看我的编辑,我已经添加了我正在使用的代码。我想你也喜欢我按“ctrl+space”&希望自动填充可以工作…但我不知道这个问题可能与抽屉布局小部件有关。。。在抽屉布局xml文件中,您必须自己键入所有内容(无自动填充)。。完全由您自己键入:android:layout\u width=“match\u parent”android:layout\u height=“match\u parent”不确定是否是这种情况,因为我已经获得了两个导入。我还是会用这个来检查我的运气。谢谢你的回复。:)我确实在线性布局上添加了gravity=“START”。然而,我错过了第二个,我会尝试一下。谢谢@vipulIt,它没用。如果有帮助的话,我已经在编辑中添加了我的代码。我相信它应该允许你这样做,我必须键入整个内容。再次感谢。还有一个问题,我希望整个页面滚动,而不仅仅是列表视图,所以我在顶部添加了Scrollview,使父线性布局作为Scrollview的子级应该只有一个子级。它可以编译,但生成的输出仅显示列表中的一项,即使屏幕上有空间。上面添加的是我的代码。
public class MainActivity extends Activity {
RelativeLayout leftRL;
RelativeLayout rightRL;
DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// I'm removing the ActionBar.
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.temp);
leftRL = (RelativeLayout)findViewById(R.id.whatYouWantInLeftDrawer);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
}
public void onOpenLeftDrawer(View view)
{
drawerLayout.openDrawer(leftRL);
}
}
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/app_background" >
<!-- The main context view -->
<FrameLayout
android:id="@+id/fragment_holder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ScrollView
android:id="@+id/leftDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:paddingBottom="3dp"
android:src="@drawable/ic_launcher"
android:text="@string/menu_account"
android:textColor="@color/nav_headingsColor"
android:textStyle="bold" />
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/nav_headingsColor" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgProfilePic"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="3"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgProfilePic"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="3"
android:clickable="true"
android:contentDescription="@null"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/lblName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Christian Bale"
android:textColor="@color/nav_textColor"
android:textSize="18sp" />
<TextView
android:id="@+id/lblEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="christianbale@imhot.com"
android:textColor="@color/nav_textColor"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:paddingBottom="3dp"
android:src="@drawable/ic_launcher"
android:text="@string/menu_menu"
android:textColor="@color/nav_headingsColor"
android:textStyle="bold" />
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/nav_headingsColor" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:layout_marginTop="2dip" >
<RadioButton
android:id="@+id/radioTopTen"
style="@style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:paddingLeft="20dp"
android:text="@string/menu_topTen" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="@color/nav_itemSep" />
<RadioButton
android:id="@+id/radioWish"
style="@style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:paddingLeft="20dp"
android:text="@string/menu_wish" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="@color/nav_itemSep" />
<RadioButton
android:id="@+id/radioGifts"
style="@style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:paddingLeft="20dp"
android:text="@string/menu_gifts" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="@color/nav_itemSep" />
<RadioButton
android:id="@+id/radioLibrary"
style="@style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:paddingLeft="20dp"
android:text="@string/menu_library" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="@color/nav_itemSep" />
<RadioButton
android:id="@+id/radioWhatsOn"
style="@style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:paddingLeft="20dp"
android:text="@string/menu_whatsOn" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="@color/nav_itemSep" />
<RadioButton
android:id="@+id/radioDownload"
style="@style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:paddingLeft="20dp"
android:text="@string/menu_download" />
</RadioGroup>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:padding="10dp"
android:src="@drawable/footer_image" />
</LinearLayout>
</ScrollView>