BottomNavigationView在模拟器中运行良好但在真实移动设备中崩溃应用程序

时间:2018-02-12 08:28:12

标签: android bottomnavigationview

我正在我的应用中实现BottomNavigationView。它在模拟器中工作正常,但当我尝试在真正的移动设备时崩溃应用程序。我也在主要活动中使用viewpager来刷滑动导航物品。我也检查gradle依赖,但仍然崩溃。和Android版本的模拟器和手机都是一样的。

主要活动

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener, Dashboard.OnFragmentInteractionListener, Profile.OnFragmentInteractionListener, Plans.OnFragmentInteractionListener, Test.OnFragmentInteractionListener {

private ViewPager vp_pages;
MenuItem prevMenuItem;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
      //  android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
      //  android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        switch (item.getItemId()) {
            case R.id.navigation_dashboard:
               // fragmentTransaction.replace(R.id.container, new Dashboard()).commit();
                vp_pages.setCurrentItem(0);
                return true;
            case R.id.navigation_profile:
               // fragmentTransaction.replace(R.id.container, new Profile()).commit();
                vp_pages.setCurrentItem(1);
                return true;
            case R.id.navigation_test:
               // fragmentTransaction.replace(R.id.container, new Test()).commit();
                vp_pages.setCurrentItem(2);
                return true;
            case R.id.navigation_plans:
                // fragmentTransaction.replace(R.id.container, new Plans()).commit();
                vp_pages.setCurrentItem(3);
                return true;
        }
        return false;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    final BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    BottomNavigationViewHelper.removeShiftMode(navigation);

  //  android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
  //  android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  //  fragmentTransaction.replace(R.id.container, new Dashboard()).commit();

    vp_pages = (ViewPager) findViewById(R.id.vp_pages);

    vp_pages.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (prevMenuItem != null) {
                prevMenuItem.setChecked(false);
            } else {
                navigation.getMenu().getItem(0).setChecked(false);
            }
            navigation.getMenu().getItem(position).setChecked(true);
            prevMenuItem = navigation.getMenu().getItem(position);

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
    setupViewPager(vp_pages);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

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

@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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_logout) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_rateus) {
        // Handle the camera action
    } else if (id == R.id.nav_feedback) {

    } else if (id == R.id.nav_contactus) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_logout) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public void onFragmentInteraction(Uri uri) {

}
private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
    Dashboard dashboard= new Dashboard();
    Profile profile = new Profile();
    Test test = new Test();
    Plans plans = new Plans();
    viewPagerAdapter.addFragment(dashboard);
    viewPagerAdapter.addFragment(profile);
    viewPagerAdapter.addFragment(test);
    viewPagerAdapter.addFragment(plans);
    viewPager.setAdapter(viewPagerAdapter);
}
}

activity_main.xml中

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

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

app_bar_main.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.smiles.elearning.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>

content_main.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.smiles.elearning.MainActivity"
tools:showIn="@layout/app_bar_main">

<FrameLayout
    android:id="@+id/container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />

<android.support.v4.view.ViewPager
    android:id="@+id/vp_pages"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 </android.support.v4.view.ViewPager>
 </android.support.constraint.ConstraintLayout>

依赖

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-v4:27.0.2'
implementation 'com.android.support:support-vector-drawable:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.android.support:design:27.0.2'
implementation 'com.android.support:support-v13:27.0.2'
compile 'com.github.lzyzsd:circleprogress:1.2.1'
compile 'dev.dworks.libs:volleyplus:+'
implementation files('libs/YouTubeAndroidPlayerApi.jar')
}

Menifest

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".SplashScreen"
        android:theme="@style/Theme.Design.Light.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Login"
        android:theme="@style/Theme.Design.Light.NoActionBar" />
    <activity
        android:name=".Register"
        android:theme="@style/Theme.Design.Light.NoActionBar" />
    <activity
        android:name=".OTP"
        android:theme="@style/Theme.Design.Light.NoActionBar" />
    <activity
        android:name=".SelectStandard"
        android:theme="@style/Theme.Design.Light.NoActionBar" />
    <activity android:name=".Course" />
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity android:name=".RegisterDetail" />
    <activity android:name=".Topics" />
    <activity android:name=".StudyMaterial" />
    <activity android:name=".Video"></activity>
</application>

logcat的

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.smiles.elearning, PID: 11772
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smiles.elearning/com.smiles.elearning.MainActivity}: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.design.widget.BottomNavigationView
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
                  at android.app.ActivityThread.access$900(ActivityThread.java:177)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:145)
                  at android.app.ActivityThread.main(ActivityThread.java:5942)
                  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:1400)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
               Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.design.widget.BottomNavigationView
                  at android.view.LayoutInflater.createView(LayoutInflater.java:640)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
                  at android.view.LayoutInflater.parseInclude(LayoutInflater.java:933)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                  at android.view.LayoutInflater.parseInclude(LayoutInflater.java:933)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                  at com.smiles.elearning.MainActivity.onCreate(MainActivity.java:64)
                  at android.app.Activity.performCreate(Activity.java:6289)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
                  at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:145) 
                  at android.app.ActivityThread.main(ActivityThread.java:5942) 
                  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:1400) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 
               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:614)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
                  at android.view.LayoutInflater.parseInclude(LayoutInflater.java:933) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                  at android.view.LayoutInflater.parseInclude(LayoutInflater.java:933) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:511) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:366) 
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                  at com.smiles.elearning.MainActivity.onCreate(MainActivity.java:64) 
                  at android.app.Activity.performCreate(Activity.java:6289) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
                  at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:145) 
                  at android.app.ActivityThread.main(ActivityThread.java:5942) 
                  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:1400) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 
               Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f070083
                  at android.content.res.Resources.getValue(Resources.java:2345)
                  at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:328)
                  at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)
                  at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
                  at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
                  at android.support.v7.view.menu.MenuItemImpl.getIcon(MenuItemImpl.java:491)
                  at android.support.design.internal.BottomNavigationItemView.initialize(BottomNavigationItemView.java:100)
                  at android.support.design.internal.BottomNavigationMenuView.buildMenuView(BottomNavigationMenuView.java:281)
                  at android.support.design.internal.BottomNavigationPresenter.updateMenuView(BottomNavigationPresenter.java:62)
                  at android.support.design.widget.BottomNavigationView.inflateMenu(BottomNavigationView.java:236)
                  at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:165)
                  at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:114)
                  at java.lang.reflect.Constructor.newInstance(Native Method) 
                  at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
                  at android.view.LayoutInflater.createView(LayoutInflater.java:614) 
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
                  at android.view.LayoutInflater.parseInclude(LayoutInflater.java:933) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                  at android.view.LayoutInflater.parseInclude(LayoutInflater.java:933) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:511) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:366) 
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                  at com.smiles.elearning.MainActivity.onCreate(MainActivity.java:64) 
                  at android.app.Activity.performCreate(Activity.java:6289) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
                  at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:145) 
                  at android.app.ActivityThread.main(ActivityThread.java:5942) 
                  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:1400) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

2 个答案:

答案 0 :(得分:0)

检查您的应用build.gradle文件并确保appcompat和设计库版本相同,如果版本不同则可能会崩溃

compile 'com.android.support:appcompat-v7:23.1.1' // appcompat library
compile 'com.android.support:design:23.1.1'       //design support library

答案 1 :(得分:0)

我唯一看到的可能不正确的是

的结束括号(/&gt;)
<android.support.design.widget.BottomNavigationView.

在某些文本编辑器(例如atom)中,需要使用左括号进行对齐。这也正是你的错误所在,所以试一试是个不错的主意。

或者,在底部通过完全写出来关闭它。例如</android.support.design.widget.BottomNavigationView>