Answer the question
In order to leave comments, you need to log in
How to fix error when recreating ViewStub?
Layout should be created on the activity by clicking on the button, for this I use ViewStub! The bottom line is this: when the button is pressed, I check whether the ViewStub exists, if not, I create it and display it on the screen, if it exists, I need to remove it!
ViewStub viewStub = null;
View v;
public void onClick(View view) {
if (viewStub == null){
viewStub = (ViewStub) finalView.findViewById(R.id.space);
v = viewStub.inflate();
}else if (viewStub != null){
v.setVisibility(View.GONE);
viewStub = null;
}
}
FATAL EXCEPTION: main java.lang.NullPointerException at com.vitaliy.useexpandablelistview.MainActivity$ListAdapter$1.onClick(MainActivity.java:167)
viewStub = null
Answer the question
In order to leave comments, you need to log in
After you called inflate() on the ViewStub, it is no longer part of the view hierarchy, meaning you can't find it a second time with finalView.findViewById(R.id.space).
But your View has already been created, it is already in the hierarchy, only it is invisible (you have hidden it with v.setVisibility(View.GONE)).
So all you have to do is check v != null at the very beginning of onClick() , in which case make it visible: v.setVisibility(View.VISIBLE);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question