Srujan Barai Srujan Barai - 6 months ago 26
Java Question

Android: animateLayoutChanges(false) programetically giving NullPointerException

This is not duplicate of 'animateLayoutChanges programetically'.

I want the default animation effects on my view changes except from the fade out effect on removing the objects. I cannot use

disableTransitionType()
because it was added in API 16. All I could think of was disabling the
android:animateLayoutChanges
to
false
, remove the view and setting it again to
true
. In my
recyclerView
I thought of removing the animation by
holder.rootView.setLayoutTansition(null)
. But unfortunately it is giving me
NullPointerException
.

A part of my code is something like this:

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
SavedMessagePackage current = data.get(position);
final String message = current.message;
holder.message.setText(message);
holder.message.setMaxLines(2);
holder.message.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.button.getVisibility() == View.GONE) {
holder.button.setVisibility(View.VISIBLE);
holder.message.setMaxLines(Integer.MAX_VALUE);
holder.message.setText(message);
}
else if (holder.button.getVisibility() == View.VISIBLE) {
holder.root_view.setLayoutTransition(null); // NullPointerException
holder.button.setVisibility(View.GONE);
holder.message.setMaxLines(2);
holder.root_view.setLayoutTransition(new LayoutTransition());
}
}
});
holder.button.setVisibility(View.GONE);
}


How can I achieve this? Let me know if the question is still unclear.

Answer

In your case you could have holder or root_view being null, but since holder was accessed successfully before, so root_view is null.

Therefore it's most likely you didn't assign holder.root_view before.

Comments