Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)

for make sure when return back & continuing previous activity i just Added & checking for getContext()!=null

Here's an good example :

Before block

adapter = new ExampleAdapter(getContext());
adapter.setData(items);
listView.setAdapter(adapter);

And better replace for getActivity()!=null

For example:

if (getActivity()!=null){
    adapter = new ExampleAdapter(getActivity());
    adapter.setData(items);
    listView.setAdapter(adapter);
}

I think this is solved all problem which got the same error like my problems !


Be conscious of where in the lifecycle you are. The value of getContext() may not be available yet.

For example, in a DialogFragment, the context will not be available until onCreateDialog() is called. So don't try and create an adapter in the constructor, because the context will still be null at that point.

Tags:

Java

Android