错误:查看android.widget.ListView不是滑动抽屉

时间:2014-03-30 08:52:30

标签: android

我在我的Android应用中使用NavigationDrawer,我已经制作了自己的自定义布局XML文件及其适配器但是当我运行我的程序时,我的应用程序崩溃了以下日志,我无法追踪原因那么,有什么建议吗?

activity_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
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <ListView
        android:id="@+id/list_slider_menu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice" />

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

MainActivity.java

public class MainActivity extends ActionBarActivity {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mActionBarDrawerToggle;

    // NavDrawer title
    private CharSequence mDrawerTitle;

    // App title
    private CharSequence mTitle;

    // slide menu items
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle = mDrawerTitle = getTitle();

        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons
        navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView)findViewById(R.id.list_slider_menu);

        navDrawerItems = new ArrayList<NavDrawerItem>();

        // adding nav drawer items to array
        // Home
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
        // Find People
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
        // Photos
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
        // Communities, Will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
        // Pages
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
        // What's hot, We  will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));


        // recyle the typed array
        navMenuIcons.recycle();

        // setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
        mDrawerList.setAdapter(adapter);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        if (savedInstanceState == null) {
            // on first time display view for first nav item
            displayView(0);
        }

        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                displayView(position);
            }
        });

    }


    @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;
    }

    private void displayView(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = new HomeFragment();
                break;
            default:
                break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

崩溃日志:

03-30 08:08:35.538    3615-3615/com.example.navigationdrawerdemo.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.navigationdrawerdemo.app/com.example.navigationdrawerdemo.app.MainActivity}: java.lang.IllegalArgumentException: View android.widget.ListView{52755e48 VFED.VC. ......I. 0,0-0,0 #7f08003e app:id/list_slider_menu} is not a sliding drawer
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalArgumentException: View android.widget.ListView{52755e48 VFED.VC. ......I. 0,0-0,0 #7f08003e app:id/list_slider_menu} is not a sliding drawer
            at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1059)
            at com.example.navigationdrawerdemo.app.MainActivity.displayView(MainActivity.java:124)
            at com.example.navigationdrawerdemo.app.MainActivity.onCreate(MainActivity.java:83)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:16)

您的ListView必须在xml中指定android:layout_gravity属性,以便DrawerLayout将其标识为滑动抽屉。 使用right,left,start,end作为值。

答案 1 :(得分:4)

我遇到了与Android Hive教程相同的问题。但在我找到一个神奇的解决方案之后。

android:layout_gravity="start"android:layout_gravity="left"放入ListView

<ListView
    android:id="@+id/listSlideMenu"
    android:layout_width="240dp"
    android:layout_height="fill_parent"
    android:layout_gravity="left" 
    android:choiceMode="singleChoice"
    android:dividerHeight="1dp"
    android:background="@color/color_primary"/>

我得到了同样的问题从androidhive学习完全使用gravity =“start”,但是在重力变化时结束它会引发错误

答案 2 :(得分:0)

  

使用DrawerLayout时确保它是 rootLayout 。不要让它成为LinearLayoutRelativeLayout的孩子。

as closeDrawer提出两个覆盖函数第一个View和第二个Gravity

通常情况下它靠近重力。所以为了关闭抽屉,你必须设置

android:layout_gravity =""
  

并且所有android:layout_gravity在DrawerLayout中都必须是唯一的

 drawer.closeDrawer(GravityCompat.START)//this code close the drawer child with layout_gravity ="start". 

如果您有其他视图,例如 linearLayout上面的listView ,请将layout_gravity提供给layout并将其从listView中删除。抽屉在重力的基础上关闭项目

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ///drawer.closeDrawer(anyViewToClose);
    drawer.closeDrawer(GravityCompat.START);//but it is better to do this using gravity.

请参阅closeDrawer代码

public void closeDrawer(@EdgeGravity int gravity, boolean animate) {
        final View drawerView = findDrawerWithGravity(gravity);
        if (drawerView == null) {
            throw new IllegalArgumentException("No drawer view found with gravity "
                    + gravityToString(gravity));
        }
        closeDrawer(drawerView, animate);
    }

另见isDrawerView

boolean isDrawerView(View child) {
        final int gravity = ((LayoutParams) child.getLayoutParams()).gravity;
        final int absGravity = GravityCompat.getAbsoluteGravity(gravity,
                ViewCompat.getLayoutDirection(child));
        if ((absGravity & Gravity.LEFT) != 0) {
            // This child is a left-edge drawer
            return true;
        }
        if ((absGravity & Gravity.RIGHT) != 0) {
            // This child is a right-edge drawer
            return true;
        }
        return false;
    }