导航抽屉,TabLayout,ViewPager,汉堡图标应用程序

时间:2016-10-09 13:22:25

标签: android android-fragments android-viewpager navigation-drawer android-tablayout

我正在创建一个带有标签和viewpager的导航抽屉。汉堡包图标未显示在工具栏上,标签标题也未显示。我尝试通过调用viewpager.setCurrentItem()方法打开导航抽屉中的一个选项卡。在发布此问题时,我的Android支持库是最新的。请查看代码并指出我的错误。

public class MainActivity extends AppCompatActivity {

private DrawerLayout drawerLayout = null;
private NavigationView navigationView = null;
private Toolbar toolbar;
private ActionBarDrawerToggle actionBarDrawerToggle;
private ViewPager viewPager;
private TabLayout tabLayout;
private ViewPagerAdapter viewPagerAdapter;
private int navItemIndex = 0;
private String[] activity_titles;

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

    getSupportActionBar();
    setSupportActionBar(toolbar);

    toolbar.setTitle("Eat Where");
    initWidgets();
    initNavMenu();
    initTabLayout();
    setUpViewPager();

    activity_titles = getResources().getStringArray(R.array.nav_item_activity_titles);

}

private void setUpViewPager() {
    viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
    viewPager.setAdapter(viewPagerAdapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}

private void initTabLayout() {
    tabLayout.addTab(tabLayout.newTab().setText("HOME"));
    tabLayout.addTab(tabLayout.newTab().setText("FAVORITES"));
    tabLayout.addTab(tabLayout.newTab().setText("NEARBY"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setupWithViewPager(viewPager);
}

private void initNavMenu() {

    actionBarDrawerToggle = new ActionBarDrawerToggle(MainActivity.this,drawerLayout,toolbar,R.string.nav_drawer_open,R.string.nav_drawer_close){
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }
    };
    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();


    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()){
                case R.id.nav_home:
                    // This line below does not jump to the requested tab
                    viewPager.setCurrentItem(item.getItemId());
                case R.id.nav_favorites:
                    viewPager.setCurrentItem(item.getItemId());
                case R.id.nav_nearBy:
                    viewPager.setCurrentItem(2);
            }
            return false;
        }
    });
}

private void initWidgets() {
    drawerLayout = (DrawerLayout) findViewById(R.id.dLayout);
    navigationView = (NavigationView) findViewById(R.id.navigationView);
    viewPager = (ViewPager) findViewById(R.id.viewPager);
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.nav_menu,menu);
    //getMenuInflater().inflate(R.menu.overflow,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id  = item.getItemId();
    switch (id){
        case R.id.menu_settings:

    }

    if (actionBarDrawerToggle.onOptionsItemSelected(item)){
        return true;
    }
    else
        return super.onOptionsItemSelected(item);
}

}

logcat的错误如下所示:

Process: com.example.joey.eatwhere, PID: 19987
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.joey.eatwhere/com.example.joey.eatwhere.Activities.MainActivity}:
     android.view.InflateException: Binary XML file line #48: Error inflating class android.support.design.widget.NavigationView
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
   at android.app.ActivityThread.access$800(ActivityThread.java:156)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:211)
   at android.app.ActivityThread.main(ActivityThread.java:5389)
   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:1020)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: android.view.InflateException: Binary XML file line #48: Error inflating class android.support.design.widget.NavigationView
   at android.view.LayoutInflater.createView(LayoutInflater.java:633)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
   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.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
   at com.example.joey.eatwhere.Activities.MainActivity.onCreate(MainActivity.java:34)
   at android.app.Activity.performCreate(Activity.java:5990)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
   at android.app.ActivityThread.access$800(ActivityThread.java:156) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:211) 
   at android.app.ActivityThread.main(ActivityThread.java:5389) 
   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:1020) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 
 Caused by: java.lang.reflect.InvocationTargetException
   at java.lang.reflect.Constructor.newInstance(Native Method)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
   at android.view.LayoutInflater.createView(LayoutInflater.java:607)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
   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.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
   at com.example.joey.eatwhere.Activities.MainActivity.onCreate(MainActivity.java:34) 
   at android.app.Activity.performCreate(Activity.java:5990) 
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332) 
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
   at android.app.ActivityThread.access$800(ActivityThread.java:156) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:211) 
   at android.app.ActivityThread.main(ActivityThread.java:5389) 
   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:1020) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class android.support.v4.widget.CircleImageView
   at android.view.LayoutInflater.createView(LayoutInflater.java:616)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
   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.support.design.internal.NavigationMenuPresenter.inflateHeaderView(NavigationMenuPresenter.java:197)
   at android.support.design.widget.NavigationView.inflateHeaderView(NavigationView.java:265)
   at android.support.design.widget.NavigationView.<init>(NavigationView.java:175)
   at android.support.design.widget.NavigationView.<init>(NavigationView.java:97)
   at java.lang.reflect.Constructor.newInstance(Native Method) 
   at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
   at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
   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.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
   at com.example.joey.eatwhere.Activities.MainActivity.onCreate(MainActivity.java:34) 
   at android.app.Activity.performCreate(Activity.java:5990) 
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332) 
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
   at android.app.ActivityThread.access$800(ActivityThread.java:156) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:211) 
   at android.app.ActivityThread.main(ActivityThread.java:5389) 
   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:1020) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
   at java.lang.Class.getConstructor(Class.java:531)
   at java.lang.Class.getConstructor(Class.java:495)
   at android.view.LayoutInflater.createView(LayoutInflater.java:580)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
   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.support.design.internal.NavigationMenuPresenter.inflateHeaderView(NavigationMenuPresenter.java:197) 
   at android.support.design.widget.NavigationView.inflateHeaderView(NavigationView.java:265) 
   at android.support.design.widget.NavigationView.<init>(NavigationView.java:175) 
   at android.support.design.widget.NavigationView.<init>(NavigationView.java:97) 
   at java.lang.reflect.Constructor.newInstance(Native Method) 
   at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
   at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
   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.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
   at com.example.joey.eatwhere.Activities.MainActivity.onCreate(MainActivity.java:34) 
   at android.app.Activity.performCreate(Activity.java:5990) 
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332) 
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
   at android.app.ActivityThread.access$800(ActivityThread.java:156) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:211) 
   at android.app.ActivityThread.main(ActivityThread.java:5389) 
   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:1020) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 
发生错误的

activity_nain.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:menu="http://schemas.android.com/apk/res-auto"
android:id="@+id/dLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.joey.eatwhere.Activities.MainActivity">

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

    <include layout="@layout/toolbar"
        android:id="@+id/layout_toolbar"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        menu:tabTextAppearance="@color/colorAccent"
        menu:tabTextColor="@color/colorAccent"
        android:layout_below="@+id/layout_toolbar"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tabs"/>

    <!--<android.support.design.widget.FloatingActionButton-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_margin="@dimen/fab_margin"-->
        <!--android:src="@android:drawable/ic_menu_share"-->
        <!--android:layout_alignParentBottom="true"-->
        <!--android:layout_alignParentRight="true"-->
        <!--android:backgroundTint="@color/colorPrimary"/>-->

</RelativeLayout>

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

<android.support.design.widget.NavigationView
    android:id="@+id/navigationView"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    menu:headerLayout="@layout/nav_header_layout"
    menu:menu="@menu/nav_menu"
    android:layout_gravity = "start"/>

我知道这是一个很长的问题,但我的项目需要这个。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果要添加抽屉布局,则必须在xml布局中添加<android.support.v4.widget.DrawerLayout>作为父标记,并在其中保留<android.support.design.widget.NavigationView>

有关详细信息,您可以访问NavigationViewDrawerLayoutThis tutorial