我正在获取E / AndroidRuntime:致命异常:main java.lang.ArrayIndexOutOfBoundsException:length = 5;指数= 5

时间:2018-10-24 13:31:42

标签: android bottomnavigationview

我正在通过遵循库https://github.com/aurelhubert/ahbottomnavigation使用底部导航栏来开发cv应用程序,但是当我运行代码时,却遇到了错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: activity.drawer.navigation.com.kitabsawticlone, PID: 21292
    java.lang.ArrayIndexOutOfBoundsException: length=5; index=5

在实现底部导航栏的代码下方

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;    
import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationAdapter;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;


public class MainActivity extends AppCompatActivity {
    private AHBottomNavigationAdapter navigationAdapter;
  private AHBottomNavigation bottomNavigation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottomNavigation = findViewById(R.id.bottom_navigation);


        AHBottomNavigation bottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_navigation);
        int[] tabColors = getApplicationContext().getResources().getIntArray(R.array.tab_colors);
        AHBottomNavigationAdapter navigationAdapter = new AHBottomNavigationAdapter(this, R.menu.bottom_navigation_menu);
        navigationAdapter.setupWithBottomNavigation(bottomNavigation, tabColors);
// Create items
        AHBottomNavigationItem item1 = new AHBottomNavigationItem(R.string.text_introduction, R.drawable.ic_introduction, R.color.color_tab_1);
        AHBottomNavigationItem item2 = new AHBottomNavigationItem(R.string.text_education, R.drawable.ic_education, R.color.color_tab_2);
        AHBottomNavigationItem item3 = new AHBottomNavigationItem(R.string.text_work, R.drawable.ic_work, R.color.color_tab_3);
        AHBottomNavigationItem item4 = new AHBottomNavigationItem(R.string.text_skills, R.drawable.ic_skills, R.color.color_tab_4);
        AHBottomNavigationItem item5 = new AHBottomNavigationItem(R.string.text_contact, R.drawable.ic_contact, R.color.color_tab_5);



        bottomNavigation.addItem(item1);
        bottomNavigation.addItem(item2);
        bottomNavigation.addItem(item3);
        bottomNavigation.addItem(item4);
        bottomNavigation.addItem(item5);

    }
}

在我的日志猫下面

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: activity.drawer.navigation.com.kitabsawticlone, PID: 21292
    java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
        at com.aurelhubert.ahbottomnavigation.AHBottomNavigation.createSmallItems(AHBottomNavigation.java:582)
        at com.aurelhubert.ahbottomnavigation.AHBottomNavigation.createItems(AHBottomNavigation.java:284)
        at com.aurelhubert.ahbottomnavigation.AHBottomNavigation.onSizeChanged(AHBottomNavigation.java:150)
        at android.view.View.sizeChange(View.java:17980)
        at android.view.View.setFrame(View.java:17942)
        at android.view.View.layout(View.java:17834)
        at android.view.ViewGroup.layout(ViewGroup.java:5754)
        at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
        at android.view.View.layout(View.java:17843)
        at android.view.ViewGroup.layout(ViewGroup.java:5754)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
        at android.view.View.layout(View.java:17843)
        at android.view.ViewGroup.layout(ViewGroup.java:5754)
        at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
        at android.view.View.layout(View.java:17843)
        at android.view.ViewGroup.layout(ViewGroup.java:5754)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
        at android.view.View.layout(View.java:17843)
        at android.view.ViewGroup.layout(ViewGroup.java:5754)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1735)
        at android.view.View.layout(View.java:17843)
        at android.view.ViewGroup.layout(ViewGroup.java:5754)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:751)
        at android.view.View.layout(View.java:17843)
        at android.view.ViewGroup.layout(ViewGroup.java:5754)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2711)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2384)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1462)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6965)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:907)
        at android.view.Choreographer.doCallbacks(Choreographer.java:709)
        at android.view.Choreographer.doFrame(Choreographer.java:644)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:893)
        at android.os.Handler.handleCallback(Handler.java:836)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:203)
        at android.app.ActivityThread.main(ActivityThread.java:6251)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1075)

2 个答案:

答案 0 :(得分:0)

检查R.array.tab_colors中有多少个项目。

答案 1 :(得分:0)

请这样替换您的代码,然后检查

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationAdapter;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;


public class MainActivity extends AppCompatActivity {
    private AHBottomNavigationAdapter navigationAdapter;
  private AHBottomNavigation bottomNavigation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottomNavigation = findViewById(R.id.bottom_navigation);     

// Create items
        AHBottomNavigationItem item1 = new AHBottomNavigationItem(R.string.text_introduction, R.drawable.ic_introduction, R.color.color_tab_1);
        AHBottomNavigationItem item2 = new AHBottomNavigationItem(R.string.text_education, R.drawable.ic_education, R.color.color_tab_2);
        AHBottomNavigationItem item3 = new AHBottomNavigationItem(R.string.text_work, R.drawable.ic_work, R.color.color_tab_3);
        AHBottomNavigationItem item4 = new AHBottomNavigationItem(R.string.text_skills, R.drawable.ic_skills, R.color.color_tab_4);
        AHBottomNavigationItem item5 = new AHBottomNavigationItem(R.string.text_contact, R.drawable.ic_contact, R.color.color_tab_5);
        bottomNavigation.addItem(item1);
        bottomNavigation.addItem(item2);
        bottomNavigation.addItem(item3);
        bottomNavigation.addItem(item4);
        bottomNavigation.addItem(item5);

           int[] tabColors = getApplicationContext().getResources().getIntArray(R.array.tab_colors);
        AHBottomNavigationAdapter navigationAdapter = new AHBottomNavigationAdapter(this, R.menu.bottom_navigation_menu);
        navigationAdapter.setupWithBottomNavigation(bottomNavigation, tabColors);



    }
}