Ich habe einen Haupt Activity.class dass ein Fragment (nicht FragmentActivity) läuft überViewPager
. Und ich habe einen benutzerdefinierten Adapter mit recyclerview Liste innerhalb des Fragments. Es gibt ein Problem beim Zugriff auf den Adapter außerhalb und innerhalb des Fragments. Also:Zugreifen Adapter innerhalb Fragment
Dieser Code vollständig funktioniert:
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();
}
Aber wenn ich rufe die gleiche Methode (updateAdapter) aus dem Haupt Activity.class gefällt das (nicht im Fragment auf Lauf):
Fragment_Questions frau = new Fragment_Questions();
frau.updateAdapter();
Code funktioniert nicht. Hier ist log:
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.remove(int)' on a null object reference
Wo 'Fragment_Questions' Fragment hinzufügen? –
Innerhalb der Activity.class, die das Fragment aufruft. 'public void openQuestion {Hinzufügen von Fragment_Questions}'. openQuestion wird innerhalb des XML-Layouts von 'onClick =" openQuestion "' ' – HEISENBERG