Java 访问片段内的适配器

Java 访问片段内的适配器,java,android,android-studio,android-fragments,Java,Android,Android Studio,Android Fragments,我有一个main Activity.class,它通过ViewPager运行一个片段而不是FragmentActivity。我有一个自定义适配器,片段中有recyclerview列表。访问片段内外的适配器时出现问题。因此: 此代码完全有效: private List<MyQuestionList> theList; private RecyclerView recyclerView; private RecyclerView.Adapter adapter; public View

我有一个main Activity.class,它通过ViewPager运行一个片段而不是FragmentActivity。我有一个自定义适配器,片段中有recyclerview列表。访问片段内外的适配器时出现问题。因此:

此代码完全有效:

private List<MyQuestionList> theList;
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;

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

    recyclerView = (RecyclerView) theview.findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);

    theList = new ArrayList<>();
    requestQueue = Volley.newRequestQueue(getContext());

    adapter = new MyQuestionsAdapter(theList, getContext());

    recyclerView.setAdapter(adapter);

    updateAdapter();  // <-- ATTENTION PLEASE, I'VE CALLED THE FUNCTION INSIDE THE FRAGMENT

}

public void updateAdapter(){ // <-- METHOD IS PUBLIC
    removeItem(0);
}

***** And there is located parser's method where items are fetching from server
and adding to recyclerview via adapter successfully.
But the code is too long, and anyway there is nothing interesting :) *****

private void removeItem(int item){
    theList.remove(item);
    adapter.notifyDataSetChanged();
}
代码不起作用。以下是日志:

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.remove(int)' on a null object reference
因为您说过已经向ViewPager添加了一个Fragment_Questions,所以使用添加的Fragment_Questions对象的引用来调用updateAdapter


在下面给出的代码中,您只是创建了一个对象,但从未将其添加到viewpager。因此,永远不会调用onCreateView方法,这会导致NullPointerException。

在调用Fragment的Activity.class内添加Fragment_质询Fragment?的位置。public void openQuestion{添加片段\问题}。openQuestion在xml布局内由onClick=openQuestion调用,但ViewPager仅控制选项卡布局,并在初始化时返回片段_问题。那么,如何实现这个方案:用户在Activity.class inits->method X中单击布局的元素->方法X调用片段中的方法?那么如何在Activity中使用它呢?
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.remove(int)' on a null object reference