2016-07-25 148 views
0

美好的一天, 我的自定義適配器沒有填充我的列表視圖。我得到這個錯誤: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; 
     } 
// 
    } 
} 

感謝您的幫助!

回答

0

錯誤是明顯的,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是正確的

+0

感謝您的回答,在設置適配器的位置下,我有一個Intent,可以在單擊子項時將應用程序發送到新類。在那個班級中,我將內容設置爲另一種佈局。我認爲這可能是問題所在。這些列表視圖不在這些佈局中的任何一箇中,它們實際上在小孩點擊發送到的標籤片段中。我不知道如何解決這個問題。我會在活動中加入這個問題。 – LBJ33

+0

我在Im試圖設置適配器的地方添加了Intent行,我還添加了它發送應用程序的類,以及這些標籤的設置位置。感謝您的幫助 – LBJ33

0

上面說的是列表視圖是空的,你可以檢查你的XML,以確保您的正確添加列表視圖。你也可以粘貼你的活動的代碼onCreate或你膨脹的xml或做setContentView。您沒有正確設置視圖。你的arraylist是好的,你的適配器代碼也看起來很好看

+0

感謝您的回答,在設置適配器的位置下,我有一個Intent,可以在單擊子項時將應用程序發送到新類。在那個班級中,我將內容設置爲另一種佈局。我認爲這可能是問題所在。這些列表視圖不在這些佈局中的任何一箇中,它們實際上在小孩點擊發送到的標籤片段中。我不知道如何解決這個問題。我會在活動中加入這個問題。 – LBJ33

+0

我已經在Im試圖設置適配器的地方添加了Intent行,我還添加了它發送應用程序的類以及選項卡的設置位置。感謝您的幫助 – LBJ33

+0

我想知道如果也許我應該做的所有代碼在切換參數的選項卡設置。唯一的問題是如何讓它知道前一個可展開列表視圖中的哪個項目被點擊,因爲每個項目都會提供不同的數據。 – LBJ33

0

錯誤很簡單。 在獲取listView的引用時,您在編寫ID時出錯。 例如R.id.list(列表是此處的ID)

也許您正在使用某個其他引用,該引用在您用於該活動的佈局的.xml中找不到。