gpt4 book ai didi

java.lang.ArrayIndexOutOfBoundsException : length=1; index=1 at forloop

转载 作者:行者123 更新时间:2023-12-01 21:41:45 28 4
gpt4 key购买 nike

我试图在我的 Activity 中膨胀一个 xml,但我在 for 循环中收到这个 Arrayoutofbondception,因为它进入循环我不知道为什么它给我这个光标错误有人可以帮忙吗?

public class ProfleList extends AppCompatActivity {
private TextView txtProfileName,txtProfileDate,txtProfileTime,txtProfileLocation;
LinearLayout linearlist ;

DatabaseHelper mydb;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profle_list);

mydb = new DatabaseHelper(this);

long count = mydb.getCount();
Log.d("count", "onCreate: "+count);

if (count == 0){

}else{
ArrayList<ItemProfile> listprofile = mydb.profilelist();
for (int i = 1; i < count; i++ ) {
ItemProfile itemProfile = listprofile.get(i);

linearlist = (LinearLayout)findViewById(R.id.layoutlist);
View[] myView = new View[i];

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView[i] = inflater.inflate(R.layout.profile_list, null);

txtProfileName = (TextView)myView[i].findViewById(R.id.txtName);
txtProfileDate = (TextView)myView[i].findViewById(R.id.txtDate);
txtProfileTime = (TextView)myView[i].findViewById(R.id.txtTime);
txtProfileLocation = (TextView)myView[i].findViewById(R.id.txtLocation);

txtProfileName.setText(itemProfile.getProfileName());
txtProfileDate.setText(itemProfile.getDay()+itemProfile.getMonth()+itemProfile.getYear());
txtProfileTime.setText(itemProfile.getHour()+itemProfile.getMinute());
txtProfileLocation.setText(itemProfile.getCity());
linearlist.addView(myView[i]);


}



}




}

}

最佳答案

您的错误在这里:

View[] myView = new View[i];
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView[i] = inflater.inflate(R.layout.profile_list, null);

你已经定义了一个包含n个元素的 View 数组,那么你不能尝试在索引n处写入......

尝试在 0 到 n-1 之间

示例:

由于您是从 1 而不是 0 开始循环,因此使用 i-1 而不是 i

for (int i = 1; i < count; i++ ) {
...
View[] myView = new View[i];
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView[i-1] = inflater.inflate(R.layout.profile_list, null);

关于java.lang.ArrayIndexOutOfBoundsException : length=1; index=1 at forloop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36375942/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com