【问题标题】:LinearLayout in navigation drawer导航抽屉中的线性布局
【发布时间】:2014-02-16 11:55:23
【问题描述】:

我想将底部 ListView 的 TextView 放在导航抽屉中。抛出java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams

main.xml

    <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"
    >

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="0dp"
        android:paddingBottom="4dp"
    />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:id="@+id/left_drawer"
        android:layout_gravity="start"
        >

        <ListView
            android:id="@+id/left_menu"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:dividerHeight="0.1dp"
            android:background="#111"
            android:divider="#FFF"
            />

        <TextView
            android:id="@+id/joke_text1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:lineSpacingExtra="5sp"
            android:text="AAAAAA"
            />
    </LinearLayout>

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

和java代码:

// Getting reference to the DrawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLinear = (LinearLayout) findViewById(R.id.left_drawer);

leftMenuItems = getResources().getStringArray(R.array.leftMenuItems);

mDrawerList = (ListView) findViewById(R.id.left_menu);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

// Generate title
String[] title = new String[] { "item1", "item2", "item4", "item4" };

// Generate icon
int[] icon = new int[] { R.drawable.abc_ic_clear, R.drawable.abc_ic_clear,
        R.drawable.abc_ic_clear, R.drawable.abc_ic_clear };


MenuListAdapter adapter = new MenuListAdapter(this, title, icon);

// Setting the adapter on mDrawerList
mDrawerList.setAdapter(adapter);

// Getting reference to the ActionBarDrawerToggle
mDrawerToggle = new ActionBarDrawerToggle(
        this,
        mDrawerLayout,
        R.drawable.ic_drawer,
        R.string.drawer_open,
        R.string.drawer_close) {

    public void onDrawerOpened(View drawerView) {
        vstitle.setText("Menu");
        invalidateOptionsMenu();
    }

    public void onDrawerClosed(View view) {
        vstitle.setText("App");
        invalidateOptionsMenu();
    }


};

// Setting DrawerToggle on DrawerLayout
mDrawerLayout.setDrawerListener(mDrawerToggle);

我找到了一些相关的主题,但对我没有帮助

  1. How to put list items at the bottom of list view in Navigation Drawer like Foursquare
  2. Adding a linear layout to Navigation drawer (ends up crashing with a ClassCastException)

【问题讨论】:

    标签: java android android-layout


    【解决方案1】:

    万岁!我在我的代码中发现了问题。 在函数 selectItem 中需要将 mDrawerLayout.closeDrawer(mDrawerList); 更改为 mDrawerLayout.closeDrawer(mDrawerLinear); 现在它完美地工作了!谢谢大家

    【讨论】:

      【解决方案2】:

      我尝试使用您提供的代码作为示例,并做了一些修改。请检查一下,如果它可能对你有帮助..

      activity_main.xml 文件

      <FrameLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/content_frame"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:paddingBottom="4dp"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="0dp" />
      
      <LinearLayout
          android:id="@+id/left_drawer"
          android:layout_width="240dp"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:orientation="vertical" >
      
          <ListView
              android:id="@+id/left_menu"
              android:layout_width="match_parent"
              android:layout_height="300dip"
              android:background="#111"
              android:choiceMode="singleChoice"
              android:divider="#FFF"
              android:dividerHeight="0.1dp" />
      
          <TextView
              android:id="@+id/joke_text1"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:lineSpacingExtra="5sp"
              android:text="AAAAAA"
              android:textColor="@android:color/white"
              android:textSize="16sp" />
      </LinearLayout>
      

      和 MainActivity.java

      package com.example.navdrawer;
      
      import android.annotation.SuppressLint;
      import android.app.Activity;
      import android.os.Bundle;
      import android.support.v4.app.ActionBarDrawerToggle;
      import android.support.v4.view.GravityCompat;
      import android.support.v4.widget.DrawerLayout;
      import android.view.Menu;
      import android.view.View;
      import android.widget.ArrayAdapter;
      import android.widget.LinearLayout;
      import android.widget.ListView;
      
      public class MainActivity extends Activity {
      private DrawerLayout mDrawerLayout;
      private LinearLayout mDrawerLinear;
      private ListView mDrawerList;
      private ActionBarDrawerToggle mDrawerToggle;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      
      
      
          mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
          mDrawerLinear = (LinearLayout) findViewById(R.id.left_drawer);
      
          //leftMenuItems = getResources().getStringArray(R.array.leftMenuItems);
      
          mDrawerList = (ListView) findViewById(R.id.left_menu);
          //mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
      
          // set a custom shadow that overlays the main content when the drawer opens
          //mDrawerLayout.setDrawerShadow(R.drawable.ic_launcher, GravityCompat.START);
      
          // Generate title
          String[] title = new String[] { "item1", "item2", "item4", "item4" };
      
          // Generate icon
          int[] icon = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher,
                  R.drawable.ic_launcher, R.drawable.ic_launcher };
      
      
          ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
              android.R.layout.simple_spinner_item, title);
      
      
      
          //MenuListAdapter adapter = new MenuListAdapter(this, title, icon);
      
          // Setting the adapter on mDrawerList
          mDrawerList.setAdapter(dataAdapter);
      
          // Getting reference to the ActionBarDrawerToggle
          mDrawerToggle = new ActionBarDrawerToggle(
                  this,
                  mDrawerLayout,
                  R.drawable.ic_launcher,
                  R.drawable.ic_launcher,
                  R.drawable.ic_launcher) {
      
              @SuppressLint("NewApi")
              public void onDrawerOpened(View drawerView) {
               //   vstitle.setText("Menu");
                  invalidateOptionsMenu();
              }
      
              @SuppressLint("NewApi")
              public void onDrawerClosed(View view) {
                //  vstitle.setText("App");
                  invalidateOptionsMenu();
              }
      
      
          };
      
          // Setting DrawerToggle on DrawerLayout
          mDrawerLayout.setDrawerListener(mDrawerToggle);
      }
      
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
      
      }
      

      【讨论】:

      • 我这边也有同样的错误。这里有完整的错误消息和堆栈跟踪pastebin.com/A6iPBrFm
      • @LONGMAN 您给定的链接未打开..请使用任何其他模式,以便我可以帮助您..
      • 什么?我不明白你
      【解决方案3】:

      如果您使用的是 Eclipse,请尝试使用Project-&gt;Clean

      另外,从您的FrameLayout 定义中删除 xml 中的 xmlns:android="http://schemas.android.com/apk/res/android 属性,因为它不是必需的。

      【讨论】:

      • 我使用的是 Android Studio。项目清理没有帮助
      • 尝试将 ListView 和 TextView 包装在一个新的 LinearLayout 中。这样,您用于菜单的 LinearLayout 实际上只有 1 个子视图而不是 2 个。
      猜你喜欢
      • 1970-01-01
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多