2014-02-07 36 views
6

我想嚮導航抽屜添加垂直線性佈局(在底部有一個標題,圖像,菜單標題,列表視圖和圖標)。然而,應用程序崩潰顯示將線性佈局添加到導航抽屜(最終因ClassCastException而崩潰)

'java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams' 

錯誤。我不確定這是否可能的直線方式,因爲我檢查了幾個鏈接,我得到的唯一結果是this.任何幫助表示讚賞。 在此先感謝。 這是我的代碼。

<!-- The main context view --> 

<FrameLayout 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<!-- The navigation drawer --> 

<LinearLayout   
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="#fff" 
    android:gravity="bottom|center" 
    android:orientation="vertical" 
    android:paddingTop="30dp" > 

    <ListView 
     android:id="@+id/left_drawer"       
     android:choiceMode="singleChoice" 
     android:divider="#E8E8E8" 
     android:dividerHeight="1dp" 
     android:footerDividersEnabled="false" 
     android:headerDividersEnabled="false" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:padding="10dp" 
     android:src="@drawable/footer_image" /> 
</LinearLayout> 

現在,這裏的問題是,XML是不是讓我來定義的寬度和高度爲ListView(元素是未知的),我也沒能爲編譯器說:「你運行它必須提供一個layout_width「屬性?請幫忙。

我的代碼與滾動視圖。

<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="[email protected]" 
         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> 
+0

我相信它應該允許你 –

+0

它的確,我必須輸入整個事情。再次感謝。 –

+0

還有一個問題,我希望整個頁面不僅滾動列表視圖,所以我添加滾動視圖頂部使父線性佈局其子作爲scrollview應該只有一個孩子。它編譯,但生成的輸出只顯示列表中的1項,即使屏幕上有空間。以上是我的代碼。 –

回答

5

我用你的答案得到了解決,謝謝:)。其實問題出在列表視圖上。我得到了左側抽屜上的線性佈局,但是它沒有正確顯示,因爲我在我的線性佈局中添加了滾動視圖,並且在同一個視圖中也有一個列表,只顯示列表中的第一個項目,留下整個屏幕空間空白。我檢查了幾個鏈接來得到答案,但它沒有奏效。最後,由於列表數據對我來說是已知的,並且不會動態改變,我添加了RadioButtons,對它們進行了定製並替換了列表。

我的代碼如下所示。

<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="[email protected]" 
         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> 

我知道這可能不是最好的可能性,但至少它的工作對我來說。如果任何人有更好的解決方案,請讓我知道。

6

做兩件事情:

  1. 設置gravity="START"到的LinearLayout ListView的不作爲的LinearLayout現在抽屜裏。
  2. 在關閉抽屜時呼叫drawer.close(Gravity.START);不要在這裏傳遞listView對象。
+0

我確實在線性佈局上加了gravity =「START」。但是我錯過了第二個,並會嘗試。謝謝@vipul –

+0

它沒有工作。如果有幫助,我在編輯中添加了我的代碼。 –

+0

它可以工作,但不顯示單選按鈕。 –

1

由於導入錯誤,此錯誤即將到來。爲佈局參數添加適當的導入。

此導入 「android.widget.LinearLayout.LayoutParams」 是越來越具有的LayoutParams即 「android.support.v4.widget.DrawerLayout $的LayoutParams」 的Drawerlayout庫聲明導致ClassCast excption混合。

+0

不確定是否屬於這種情況,因爲我有兩個進口。將仍然檢查我的運氣與此。感謝4反射。 :) –

13

您的查詢是很難不代碼&堆棧跟蹤瞭解,但無論如何..

記住android.support.v4.widget.DrawerLayout只能有3個元素(正是這個順序):

  1. 你的主要頁面

  2. 左抽屜

  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> 

你的活動:

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); 
    } 
} 
+0

對不起,在代碼部分。我會記下它的未來問題。 :)謝謝你的答案,我會試試看,並告訴你它是否有效。我想我的代碼中唯一缺少的東西是openDrawer()方法,我傳遞參數視圖本身。 –

+0

你能告訴我,如果我可以創建一個單獨的抽屜xml並使用在DrawerLayout中調用它? –

+1

我還沒有嘗試,但我會避免這種情況,因爲DrawerLayout小部件遵循非常嚴格的規則,即在頁面中具有最多3個元素...... PS:在左側抽屜視圖上方的可以充滿任何事物....我無法想象除此之外的任何場景。 –

0

嘗試這樣做,

在功能選擇信息, 變化mDrawerLayout.closeDrawer(mDrawerList);到 mDrawerLayout.closeDrawer(mDrawerLinear); 應該工作得很好。

因爲引用現在應該是您的linearLayout,它不再是您正在關閉或打開的列表視圖。