没有列表的Android导航抽屉

时间:2015-04-20 23:11:49

标签: android listview layout static navigation-drawer

我试图在不使用列表和列表适配器的情况下构建导航抽屉。

我想在导航栏中只有一个静态布局。

我使用Android Studio为Navigation Drawer项目生成了代码存根。我能够编辑" fragment_navigation_drawer.xml" - 但是当我在listView之外添加任何东西时,我在MainActivity中得到以下异常

例外:

java.lang.RuntimeException: Unable to start activity ComponentInfo{perltek.myTransit.nycMaps/perltek.myTransit.nycMaps.MainActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at perltek.myTransit.nycMaps.MainActivity.onCreate(MainActivity.java:40)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException
            at java.lang.VMClassLoader.findLoadedClass(Native Method)
            at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:362)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:499)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:454)
            at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2123)
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
            at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:547)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at perltek.myTransit.nycMaps.MainActivity.onCreate(MainActivity.java:40)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-2

主要活动代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);  // Exception on this line
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();

// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));

}

主要活动XML:

<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
     the container. -->
<fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
    android:layout_gravity="start"

    tools:layout="@layout/fragment_navigation_drawer" />

如何在导航抽屉中仅使用静态布局替换listView?

感谢。

1 个答案:

答案 0 :(得分:1)

我用相对布局替换了列表视图。以下xml允许我有一个从左侧滑入的顶部和底部菜单。我还在底部菜单下面有版本文本(如果你想知道那里的文本视图)。我不确定,但我认为您可以使用从View Group派生的任何视图替换List视图。

         

    <ImageView
        android:id="@+id/playerBGFill"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:scaleType="centerCrop"
        android:layout_marginLeft="@dimen/main_neg_10sp"
        android:layout_marginRight="@dimen/main_neg_10sp"
        android:layout_marginTop="@dimen/main_neg_10sp"
        android:src="@drawable/bg_texture2"
        android:alpha="0.6"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include layout="@layout/login_screen"/>
        <include layout="@layout/system_list_popup"/>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/mainView">

            <include layout="@layout/nightlife_titlebar"/>

            <com.hdms.manager.Fragments.NightlifeViewPager
                android:id="@+id/contentViewPager"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <LinearLayout android:id="@+id/pagination"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
            </LinearLayout>

        </LinearLayout>

    </RelativeLayout>
</FrameLayout>

<!-- navigation drawer -->
<FrameLayout android:id="@+id/left_drawer"
          android:layout_width="@dimen/main_280sp"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:choiceMode="singleChoice"
          android:divider="@color/Red"
          android:dividerHeight="0dp"
          android:background="@drawable/menudrawer_bg">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView android:id="@+id/left_drawer_menu"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentTop="true"
                android:layout_above="@+id/left_drawer_bottom"/>

        <ListView android:id="@+id/left_drawer_bottom"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_above="@+id/versionText"/>

        <TextView android:id="@+id/versionText"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:gravity="center"
                  android:textSize="@dimen/main_12sp"
                  android:text="Version:"
                  android:textAlignment="center"
                  android:textColor="@color/GreyMid"
                  android:layout_alignParentBottom="true"/>
    </RelativeLayout>
</FrameLayout>

希望这有帮助。

以下是创建菜单的代码。

private void createDrawerLayout(){
    if (mDrawerList != null)
        return;

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer_menu);
    mDrawerListBottom = (ListView) findViewById(R.id.left_drawer_bottom);
    mDrawerMenu = (FrameLayout) findViewById(R.id.left_drawer);

    try
    {
        String app_ver = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0).versionName;
        TextView version = (TextView)findViewById(R.id.versionText);
        version.setText("Version : " + app_ver);
    }
    catch (PackageManager.NameNotFoundException e)
    {
        e.printStackTrace();
    }

    List<String> menuItems = new Vector<String>();
    setMainMenu(menuItems);

    menuItems = new Vector<String>();
    menuItems.add("Go to Preview Song");
    menuItems.add("Display Always On");

    final NightlifeBottomDrawerAdaptor drawerAdaptor = new NightlifeBottomDrawerAdaptor<String>(getContext(), R.layout.drawer_menu_item, menuItems);
    drawerAdaptor.setItemChecked(1, mDisplayOn);
    mDrawerListBottom.setAdapter(drawerAdaptor);

    // Set the list's click listener
    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> aAdapterView, View aView, int aPosition, long aID) {
            if(  aPosition >= Fragments.MaxFragments.ordinal() )
                ++aPosition; // to skip 'MaxFragments'
            switch (Fragments.values()[aPosition]) {
                case Logout:
                    saveCreditData();
                    jumpToLogin();
                    mDrawerLayout.closeDrawer(mDrawerMenu);
                    break;
                case MusicZone:
                    if (!App.mIsKioskApp)
                    {
                        jumpToMusicZoneList(null, false, true);
                        mDrawerLayout.closeDrawer(mDrawerMenu);
                    }
                    else
                        showSettingsPasswordDialog(aView);
                    break;

                case Controls:
                case Search:
                case BAU:
                    final int position = aPosition;
                    mRunnableHandler.post(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            if (mLandscapeMode)
                                onControlPressed();
                            else
                            {
                                mPreviousFragment = mCurrentFragment;
                                mCurrentFragment = position;
                            }

                            mUndoPage.clear();
                            UpdateDisplay();
                            mTitleBar.updateSearchButton();
                            mTitleBar.setCreditText();
                        }
                    });
                    setDrawAdaptor(aPosition);
                    mDrawerList.setItemChecked (aPosition, true);
                    mDrawerLayout.closeDrawer(mDrawerMenu);
                    break;
            }
        }
    });

    mDrawerListBottom.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> aAdapterView, View aView, final int aPosition, long aID)
        {
            switch (aPosition)
            {
                case 1:
                    mDisplayOn = !mDisplayOn;
                    if (mScreenDimmer != null){
                        mScreenDimmer.cancel();
                        mScreenDimmer = null;
                    }
                    setDisplayOnFlag();
                    startScreenDimmerTimer();
                    drawerAdaptor.setItemChecked(aPosition, mDisplayOn);
                    break;
                case 0:
                    mDrawerListBottom.setItemChecked (Fragments.Search.ordinal(), true);
                    mDrawerLayout.closeDrawer(mDrawerMenu);

                    final int Position = mJumpFragment.ordinal();
                    mRunnableHandler.post(new Runnable(){
                        @Override
                        public void run(){
                            if ( (mBAUFragment != null && mBAUFragment.get() != null && mJumpFragment == Fragments.BAU) ||
                                    (mSearchFragment != null && mSearchFragment.get() != null && mJumpFragment == Fragments.Search) ) {
                                mPreviousFragment = mCurrentFragment;
                                mCurrentFragment = Position;

                                if(mJumpFragment == Fragments.BAU)
                                    mBAUFragment.get().jumpTo(mJumpPosition);
                                else
                                    mSearchFragment.get().jumpTo(mJumpPosition);

                                mUndoPage.clear();
                                UpdateDisplay();
                            }
                        }
                    });
                    mDrawerListBottom.setItemChecked(mCurrentFragment, true);
                    mDrawerLayout.closeDrawer(mDrawerMenu);
                    break;
            }
        }
    });