Issue
I'm getting the following error when trying to autocompleteTextView.showDropDown()
:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
I've tried to do this in various Fragment
lifecycle methods. This error always pops up.
Where do I call methods that display additional windows in a fragment?
EDIT:
@BindView(R.id.acService) AutoCompleteTextView autocompleteSTextView;
@Override
public void onAttach(Context context)
{
super.onAttach(context);
this.context = context;
}
@Override
public void onDetach()
{
super.onDetach();
context = null;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_home, container, false);
}
@Override
public void onResume()
{
initialize();
loadSkillsData();
super.onResume();
}
private void initialize()
{
util = new Util(context);
requestService = new RequestService();
requestService.setServerUserId(getUser().getServerUserId());
geoDataClient = Places.getGeoDataClient(context);
autocompleteAdapter = new PlaceAutocompleteAdapter(context, geoDataClient, BOUNDS_WORLD, null);
autocompleteTextView.setAdapter(autocompleteAdapter);
mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapFragment);
mapFragment.getMapAsync(this);
autocompleteTextView.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View view, boolean hasFocus)
{
if(hasFocus)
{
autocompleteService.showDropDown();
}
}
});
}
And here's how the fragment is loaded in the Activity
. I'm using the MaterialNavigationDrawer:
private void replaceWithFragment(Fragment fragment)
{
getSupportFragmentManager().beginTransaction().replace(R.id.frame, fragment).commit();
navigationDrawer.closeDrawer();
}
Solution
onFocusChange()
can be called before anything is actually visible.
Change
if(hasFocus)
{
autocompleteService.showDropDown();
}
to
if(hasFocus && isVisible())
{
autocompleteService.showDropDown();
}
This will make sure the fragment is actually showing and added to a Window before you try to show the dropdown.
Answered By - TheWanderer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.