美好的一天, 我的自定义适配器没有填充我的listviews有问题。我收到这个错误:
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是空的..这就是我称之为适配器的地方:
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(发送意图的地方)的一部分(我已经包含了on create方法以及设置的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;
}
//
}
}
感谢您的帮助!
答案 0 :(得分:0)
错误很明显,listView对象之一为null:
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是否正确
答案 1 :(得分:0)
它表示列表视图为空,您可以检查您的xml以确保正确添加列表视图。您是否还可以将活动代码粘贴到“创建”或“xml膨胀”或setContentView
。您没有正确设置视图。你的arraylists很好,你的适配器代码似乎也很好看
答案 2 :(得分:0)
错误很简单。 在获取listView的引用时,您在编写ID时遇到了一些错误。 例如R.id.list(列表是这里的ID)
也许您正在使用您在布局的.xml中找不到的其他参考资料。