Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从数组中显示下一项,而不是预期值_Java_Android_Arrays_Arraylist - Fatal编程技术网

Java 从数组中显示下一项,而不是预期值

Java 从数组中显示下一项,而不是预期值,java,android,arrays,arraylist,Java,Android,Arrays,Arraylist,这是一个非常基本的java问题,我有一个字符串数组 String[] navTitles = new String[] {Home, HR, company, joiner, next}; 这些标题显示在列表视图中。单击第一个项目时,我必须显示某个列表,否则,如果单击任何其他标题,我必须显示不同的列表。第一个(默认)列表应该是单击第一个项目时显示的列表。问题是,如果单击某个标题,将显示与下一个标题相关的列表。单击最后一个标题时,出现错误:- 05-02 04:47:38.787: E/

这是一个非常基本的java问题,我有一个字符串数组

String[] navTitles = new String[] {Home, HR, company, joiner, next};
这些标题显示在列表视图中。单击第一个项目时,我必须显示某个列表,否则,如果单击任何其他标题,我必须显示不同的列表。第一个(默认)列表应该是单击第一个项目时显示的列表。问题是,如果单击某个标题,将显示与下一个标题相关的列表。单击最后一个标题时,出现错误:-

    05-02 04:47:38.787: E/AndroidRuntime(1490): java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
05-02 04:47:38.787: E/AndroidRuntime(1490):     at com.abc.attini.Home.displayView(Home.java:600)
这是我的密码:--

for(int j=0;j
尝试以下代码:-

发生错误,因为索引值大于索引值

private void displayView(int position) 
{
    Fragment fragment = null;
    position = position % length; //length of your array 

    if(position==0)
    {
        fragment = new HomeFragment(SPHostUrl,encodedAccountName,deviceAuthKey,usersname,avatarUrl, fullName,getApplicationContext(),myFinalNewsList);
    }
    else
    {
        String myPosition = null;

        myPosition =  navDrawerItems.get(position).getTitle(); //Line 600


        companyNewsList = Lists.newArrayList(Collections2.filter(myFinalNewsList, new ArticleFilter(myPosition)));
         fragment = new HomeFragment(SPHostUrl,encodedAccountName,deviceAuthKey,usersname,avatarUrl, fullName,getApplicationContext(),companyNewsList);

    }
}

上面的代码在我这方面效果很好,因为它在数组长度下创建位置。

哪一行是
Home.java
class的第600行?@Hamid请检查更新是否确实将正确的位置值传递给函数?@Swayam当我单击第二个标题时,我得到位置=2,而我应该得到位置=1是,我理解。但我要问的是,你是否将正确的值传递给函数?你能在发送之前记录值吗?@user3534519你的长度是多少?数组的长度是5。让你在java文件中检查此代码,因为在我这端运行平稳
private void displayView(int position) 
{
    Fragment fragment = null;
    position = position % length; //length of your array 

    if(position==0)
    {
        fragment = new HomeFragment(SPHostUrl,encodedAccountName,deviceAuthKey,usersname,avatarUrl, fullName,getApplicationContext(),myFinalNewsList);
    }
    else
    {
        String myPosition = null;

        myPosition =  navDrawerItems.get(position).getTitle(); //Line 600


        companyNewsList = Lists.newArrayList(Collections2.filter(myFinalNewsList, new ArticleFilter(myPosition)));
         fragment = new HomeFragment(SPHostUrl,encodedAccountName,deviceAuthKey,usersname,avatarUrl, fullName,getApplicationContext(),companyNewsList);

    }
}