ListView未填充数据
美好的一天, 我的自定义适配器没有填充我的列表视图。我得到这个错误:ListView未填充数据
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.bestworkouts.sheikoworkout.MyWorkout$1.onChildClick(MyWorkout.java:64)
让我觉得我的数组列表是空的。这就是我所说的适配器:
ArrayList<CustomObject> w29 = new ArrayList<CustomObject>();
w29.add(new CustomObject("Squat", "65%", "6", "150", false));
final CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(this, w29);
expandableListView = (ExpandableListView) findViewById(R.id.listViewWorkouts);
Workouts_details = DataProvider.getInfo();
Workout_list = new ArrayList<>(Workouts_details.keySet());
mAdapter = new WorkoutsAdapter(this, Workouts_details, Workout_list);
final ListView listViewFri = (ListView) findViewById(R.id.listViewFri);
final ListView listViewMon = (ListView) findViewById(R.id.listViewMon);
final ListView listViewWed = (ListView) findViewById(R.id.listViewWed);
expandableListView.setAdapter(mAdapter);
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if (groupPosition == 0) {
if (childPosition == 0) {
listViewMon.setAdapter(customListViewAdapter);
listViewFri.setAdapter(customListViewAdapter);
listViewWed.setAdapter(customListViewAdapter);
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
startActivity(intent);
这是适配器:
public class CustomListViewAdapter extends BaseAdapter {
private LayoutInflater inflater;
public ArrayList<CustomObject> objects;
boolean[] checkBoxState;
CustomObject data[] = null;
private class ViewHolder {
TextView txtExercise;
TextView txtPercent;
TextView txtReps;
TextView txtWeight;
CheckBox check1;
}
public void addAdapterItem(CustomObject item) {
objects.add(item);
}
public CustomListViewAdapter(Context context, ArrayList<CustomObject> objects) {
inflater = LayoutInflater.from(context);
this.objects = objects;
checkBoxState= new boolean[objects.size()];
}
public int getCount() {
return objects.size();
}
public CustomObject getItem(int position) {
return objects.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.workout_item, null);
holder.txtExercise = (TextView) convertView.findViewById(R.id.txtExercise);
holder.txtPercent = (TextView) convertView.findViewById(R.id.txtPercentage);
holder.txtReps = (TextView) convertView.findViewById(R.id.txtReps);
holder.txtWeight = (TextView) convertView.findViewById(R.id.txtWeight);
holder.check1 = (CheckBox) convertView.findViewById(R.id.check1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtExercise.setText(objects.get(position).getExercise());
holder.txtPercent.setText(objects.get(position).getPercent());
holder.txtReps.setText(objects.get(position).getReps());
holder.txtWeight.setText(objects.get(position).getWeight());
holder.check1.setChecked(checkBoxState[position]);
holder.check1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(((CheckBox)v).isChecked()) {
checkBoxState[position] = true;
} else {
checkBoxState[position] = false;
}
}
});
return convertView;
}
}
这是customobject分类:
public class CustomObject {
private String exercise;
private String percent;
private String reps;
private String weight;
private boolean check1;
public CustomObject(String exercise, String percent, String reps, String weight, boolean check1) {
this.exercise = exercise;
this.percent = percent;
this.reps = reps;
this.weight = weight;
this.check1 = check1;
}
public String getExercise() {
return exercise;
}
public String getPercent() {
return percent;
}
public String getReps() {
return reps;
}
public String getWeight() {
return weight;
}
// public CheckBox getCheck1() {
// return check1;
// }
}
这是WorkoutDaysActivity(发送意图的地方)的一部分(我已经包含了创建方法以及3个选项卡的设置。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_days);
mToolBar = activateToolbarWithHomeEnabled();
setUpNavigationDrawer();
// setSupportActionBar(toolbar);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
View rootView = inflater.inflate(R.layout.fragment_workout_days, container, false);
// CustomListViewAdapter mCustomListViewAdapter;
// mCustomListViewAdapter = new CustomListViewAdapter(this,)
// ListView listViewMon = (ListView) rootView.findViewById(R.id.listViewMon);
// listViewMon.setAdapter(mCustomListViewAdapter);
return rootView;
case 2: View rootView2 = inflater.inflate(R.layout.fragment_sub_page1, container, false);
TextView textView2 = (TextView) rootView2.findViewById(R.id.textView2);
textView2.setText("Workout 29 Week 1");
return rootView2;
case 3:
View rootView3 = inflater.inflate(R.layout.fragment_sub_page2, container, false);
TextView textView3 = (TextView) rootView3.findViewById(R.id.txtFrag3);
textView3.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView3;
default: View rootView4 = inflater.inflate(R.layout.fragment_workout_days, container, false);
TextView textView4 = (TextView) rootView4.findViewById(R.id.txtFrag1);
textView4.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView4;
}
//
}
}
感谢您的帮助!
错误是明显的,ListView的对象之一为空:
final ListView listViewFri = (ListView) findViewById(R.id.listViewFri);
final ListView listViewMon = (ListView) findViewById(R.id.listViewMon);
final ListView listViewWed = (ListView) findViewById(R.id.listViewWed);
支票上的观点ID是正确的
上面说的是列表视图是空的,你可以检查你的XML,以确保您的正确添加列表视图。你也可以粘贴你的活动的代码onCreate或你膨胀的xml或做setContentView
。您没有正确设置视图。你的arraylist是好的,你的适配器代码也看起来很好看
感谢您的回答,在设置适配器的位置下,我有一个Intent,可以在单击子项时将应用程序发送到新类。在那个班级中,我将内容设置为另一种布局。我认为这可能是问题所在。这些列表视图不在这些布局中的任何一个中,它们实际上在小孩点击发送到的标签片段中。我不知道如何解决这个问题。我会在活动中加入这个问题。 – LBJ33
我已经在Im试图设置适配器的地方添加了Intent行,我还添加了它发送应用程序的类以及选项卡的设置位置。感谢您的帮助 – LBJ33
我想知道如果也许我应该做的所有代码在切换参数的选项卡设置。唯一的问题是如何让它知道前一个可展开列表视图中的哪个项目被点击,因为每个项目都会提供不同的数据。 – LBJ33
错误很简单。 在获取listView的引用时,您在编写ID时出错。 例如R.id.list(列表是此处的ID)
也许您正在使用某个其他引用,该引用在您用于该活动的布局的.xml中找不到。
感谢您的回答,在设置适配器的位置下,我有一个Intent,可以在单击子项时将应用程序发送到新类。在那个班级中,我将内容设置为另一种布局。我认为这可能是问题所在。这些列表视图不在这些布局中的任何一个中,它们实际上在小孩点击发送到的标签片段中。我不知道如何解决这个问题。我会在活动中加入这个问题。 – LBJ33
我在Im试图设置适配器的地方添加了Intent行,我还添加了它发送应用程序的类,以及这些标签的设置位置。感谢您的帮助 – LBJ33