gpt4 book ai didi

使用listview时出现android arrayindexoutofbounds异常

转载 作者:行者123 更新时间:2023-11-29 21:38:37 25 4
gpt4 key购买 nike

我收到一个 arrayindexoutofbounds 异常。我的适配器代码是

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LevelAdapter extends ArrayAdapter<Level> {

static Context context;
static int layoutResourceId;
static Level data[] = null;

public LevelAdapter(Context context, int layoutResourceId, Level[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}

/* public LevelAdapter(ShowFrag1 showFrag1, int listItem,
Level[] weather_data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;

// TODO Auto-generated constructor stub
}*/

public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;

if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);

holder = new WeatherHolder();
// holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txt);
// holder.imgIcon2=(ImageView)row.findViewById(R.id.imgIcon2);
Typeface robotoLight = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Light.ttf");
holder.txtTitle.setTypeface(robotoLight);

row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}

Level weather = data[position];
holder.txtTitle.setText(weather.title);
// holder.imgIcon.setImageResource(weather.icon);

return row;
}

static class WeatherHolder
{
// ImageView imgIcon;
TextView txtTitle;
// ImageView imgIcon2;
}

}

使用 ListView 的 fragment 代码是这样的

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AboutFrag3 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.aboutfrag3, container,false);
ListView lv1=(ListView) v.findViewById(R.id.listView1);
ListView lv2=(ListView)v.findViewById(R.id.listView2);
Level weather_data[] = new Level[]
{

new Level(R.drawable.sac1, "O63", R.drawable.play_button),
new Level(R.drawable.sn2, "O26",R.drawable.play_button),
new Level(R.drawable.sn3, "O",R.drawable.play_button),
new Level(R.drawable.sn4, "O83",R.drawable.play_button),
new Level(R.drawable.sn6, "O.23",R.drawable.play_button),
new Level(R.drawable.sn6, "O9",R.drawable.play_button),
new Level(R.drawable.sn6, "O96",R.drawable.play_button),
new Level(R.drawable.sn6, "T98",R.drawable.play_button),
new Level(R.drawable.sn6, "T37",R.drawable.play_button),
new Level(R.drawable.sn6, "T248",R.drawable.play_button),
new Level(R.drawable.sn6, "T.87",R.drawable.play_button),
new Level(R.drawable.s6, "Tee: 54.06",R.drawable.play_button),
new Level(R.drawable.sn6, "T1",R.drawable.play_button),
new Level(R.drawable.s6, "Te",R.drawable.play_button)


};
Level weather_data2[] = new Level[]
{
new Level(R.drawable.s1,"O3",R.drawable.play_button),
new Level(R.drawable.s1,"O 154",R.drawable.play_button),
new Level(R.drawable.s1,"O2",R.drawable.play_button),
new Level(R.drawable.sh1,"O: 5.11",R.drawable.play_button),
new Level(R.drawable.s1, "Te8",R.drawable.play_button),
new Level(R.drawable.s1, "T5",R.drawable.play_button),
new Level(R.drawable.s1, "T-10",R.drawable.play_button),
new Level(R.drawable.s1, "T52",R.drawable.play_button)
};
LevelAdapter adapter = new LevelAdapter(getActivity(),
R.layout.list_item, weather_data);
LevelAdapter adapter2 = new LevelAdapter(getActivity(), R.layout.list_item, weather_data2);

View header = inflater.inflate(R.layout.header2, null);
//View header2 = inflater.inflate(R.layout.header, null);
View header3 = inflater.inflate(R.layout.header3, null);
lv1.addHeaderView(header);
lv2.addHeaderView(header3);
lv1.setAdapter(adapter);
lv2.setAdapter(adapter2);
Utility.setListViewHeightBasedOnChildren(lv1);
Utility.setListViewHeightBasedOnChildren(lv2);
return v;
}
}

我在这一行中收到 arrayindexoutofbounds 异常

    Level weather = data[position];

我哪里错了?谢谢

最佳答案

这是因为你的data[]static。当您将属性设置为 static 时,它会使从该类派生的所有对象的属性都相同。要修复它,只需使数据不是static

关于使用listview时出现android arrayindexoutofbounds异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17733609/

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