Skip to content Skip to sidebar Skip to footer

Attempt To Invoke Virtual Method 'java.lang.Object Android.content.Context.getSystemService(java.lang.String)

i want to ask, why my code doesnt work when running on device, but when i run on emulator android like a Genymotion, it's working perfectly.. someone said on this link like this :

Solution 1:

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 !


Solution 2:

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.


Solution 3:

Actually this issue come when something comes as null byte. and when we are using recycler view then it comes mostly. Because most people confuse in onCreateView of RecyclerViewAdpater. Some code writers write Layout inflater as the layout of Tab. but here we will use another layout where we will mention our internal Recyclerview layout.

view= LayoutInflater.from(mcontext).inflate(R.layout.**item_tab14**,parent,false);

Post a Comment for "Attempt To Invoke Virtual Method 'java.lang.Object Android.content.Context.getSystemService(java.lang.String)"