来自其他类的Arraylist为空

时间:2018-10-30 13:45:40

标签: java class arraylist constructor

我正在尝试从其他类访问Arraylist。此方法有效,但Arraylist始终为空。

public class CategoryFragment extends Fragment {

private List<Category> lsCategory;

public CategoryFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    lsCategory = new ArrayList<>();

    lsCategory.add(new Category(1, getString(R.string.cat_school), R.drawable.cat_school_filled_48dp));
    lsCategory.add(new Category(2, getString(R.string.cat_food), R.drawable.cat_food_filled_48dp));
    lsCategory.add(new Category(3, getString(R.string.cat_sports), R.drawable.cat_sports_filled_48dp));
    lsCategory.add(new Category(4, getString(R.string.cat_travel), R.drawable.cat_travel_filled_48dp));
    lsCategory.add(new Category(5, getString(R.string.cat_hobbies), R.drawable.cat_sports_filled_48dp));
    lsCategory.add(new Category(6, getString(R.string.cat_countries), R.drawable.cat_countries_filled_48dp));
    lsCategory.add(new Category(7, getString(R.string.cat_numbers), R.drawable.cat_countries_filled_48dp));
    lsCategory.add(new Category(8, getString(R.string.cat_animals), R.drawable.cat_animals_filled_48dp));
    lsCategory.add(new Category(9, getString(R.string.cat_nature), R.drawable.cat_nature_filled_48dp));

    View v = inflater.inflate(R.layout.fragment_category, container, false);
    RecyclerView myRv = (RecyclerView) v.findViewById(R.id.main_recyclerView);
    CategoryRecyclerViewAdapter myAdapter = new CategoryRecyclerViewAdapter(getActivity(), lsCategory);
    myRv.setLayoutManager(new GridLayoutManager(getActivity(), 3));
    myRv.setAdapter(myAdapter);
    return v;
}

public List<Category> getLsCategory() {
    return lsCategory;
}

}

在另一个课程中,我尝试访问此Arraylist。

public class FlashcardFragment extends Fragment {

private List<Flashcard> lsFlashcard;
private CategoryFragment categoryFragment;

public FlashcardFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    lsFlashcard = new ArrayList<>();
    categoryFragment = new CategoryFragment();
    List<Category> calledList = categoryFragment.getLsCategory();

    if(calledList != null && !calledList.isEmpty()){
        for(int i = 0; i < calledList.size(); i++){
            int number = calledList.get(i).getId();

            if(number == 1){
                lsFlashcard.add(new Flashcard(1, getString(R.string.school_backpack), R.drawable.school_backpack));
                lsFlashcard.add(new Flashcard(2, getString(R.string.school_textbook), R.drawable.school_textbook));
                lsFlashcard.add(new Flashcard(3, getString(R.string.school_book), R.drawable.school_book));
                lsFlashcard.add(new Flashcard(4, getString(R.string.school_workbook), R.drawable.school_workbook));
                lsFlashcard.add(new Flashcard(5, getString(R.string.school_file), R.drawable.school_file));
            }
            else if(number == 2){
                lsFlashcard.add(new Flashcard(1, getString(R.string.school_backpack), R.drawable.school_backpack));
                lsFlashcard.add(new Flashcard(2, getString(R.string.school_textbook), R.drawable.school_textbook));
                lsFlashcard.add(new Flashcard(3, getString(R.string.school_book), R.drawable.school_book));
                lsFlashcard.add(new Flashcard(4, getString(R.string.school_workbook), R.drawable.school_workbook));
                lsFlashcard.add(new Flashcard(5, getString(R.string.school_file), R.drawable.school_file));
            }
            else{
                lsFlashcard.add(new Flashcard(1, getString(R.string.school_backpack), R.drawable.school_backpack));
                lsFlashcard.add(new Flashcard(2, getString(R.string.school_textbook), R.drawable.school_textbook));
                lsFlashcard.add(new Flashcard(3, getString(R.string.school_book), R.drawable.school_book));
                lsFlashcard.add(new Flashcard(4, getString(R.string.school_workbook), R.drawable.school_workbook));
                lsFlashcard.add(new Flashcard(5, getString(R.string.school_file), R.drawable.school_file));
            }
        }
    }
    else{

    }

    View v = inflater.inflate(R.layout.fragment_flashcard, container, false);
    RecyclerView myRv = (RecyclerView) v.findViewById(R.id.main_recyclerView);
    FlashcardRecyclerViewAdapter myAdapter = new FlashcardRecyclerViewAdapter(getActivity(), lsFlashcard);
    myRv.setLayoutManager(new GridLayoutManager(getActivity(), 3));
    myRv.setAdapter(myAdapter);
    return v;
}

}

我得到的错误是:

java.lang.NullPointerException:尝试在空对象引用上调用接口方法'boolean java.util.List.add(java.lang.Object)'

1 个答案:

答案 0 :(得分:1)

这是因为您正在创建CategoryFragment的新实例并调用其getLsCategory();方法

categoryFragment = new CategoryFragment();
List<Category> calledList = categoryFragment.getLsCategory();

这里 private List<Category> lsCategory;是一个实例变量,因此该类的每​​个实例将拥有自己的lsCategory副本。

如果您希望所有实例的该变量都相同,请将其设置为静态private static List<Category> lsCategory;