繁体   English   中英

无法访问的代码-不应无法访问

[英]Unreachable Code - Shouldn't be unreachable

我想用从文件中读取的数据填充自定义列表视图。 使用方法ReadPeopleDetails读取文件。 这将返回一个arraylist,然后我使用循环使用名称字符串并将其放入arraylist people_name中。

但是,显然是由于for循环, final ArrayAdapter适配器行无法访问。 第二双眼睛能告诉我为什么吗?

我认为for循环将结束(长度由arraylist的长度指定)

码:

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.add_party__people_list, menu);
    ArrayList<PeopleDetails> people_list = Readpeopledetails();
    ArrayList<String> people_name = null; ///this will catch the names from the above line, and put them into
    ///an arraylist for use in populating the ListView
    int i = people_list.size();
    //int j; acts as counter in iterator system
    PeopleDetails[] people_array = (PeopleDetails[]) people_list.toArray();
    for(int j = people_list.size(); /*not using termination*/ ; j++ ){
        people_name.add(people_array[j].name);

    }

    final ArrayAdapter adapter = new ArrayAdapter(this, R.layout.person_list_item, people_name);
    return true;
}
for(int j = people_list.size(); /*not using termination - SO LOOP WONT END*/ ; j++ ){

这是从列表的末尾开始,然后从那里开始递增,因此它将永远持续下去。 你可能想要...

for(int j = 0; j < i ; j++ ){

没有结束循环的条件。 所以现在是一个无限循环..所以下一行不可达

做类似的事情

for(int j = 0; j< people_list.size(); j++ ){
        people_name.add(people_array[j].name);

    }

似乎您是Java新手。 要了解有关for循环的更多信息,请参见此处的文档

除了循环不会终止之外,people_name从未分配给ArrayList,因此当您尝试在其上调用add方法时,您将获得NullPointerException。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM