Android 在SwipeRefreshLayout@v23.2.0上切换选项卡时的NPE
将支持库更新到23.2.0版后Android 在SwipeRefreshLayout@v23.2.0上切换选项卡时的NPE,android,android-support-library,Android,Android Support Library,将支持库更新到23.2.0版后 compile 'com.android.support:design:23.2.0' 在选项卡之间切换时,我开始出现此错误(Android版本5.0.2): 看起来像是一个支持库错误,有人也有这个问题吗?有什么解决方法吗?检查库中所有可能覆盖某些资源的“void android.graphics.drawable.drawable.setAlpha(int)”选项。void setAlpha(null)我没有看到这个问题,但是如果它确实在SwipeRefres
compile 'com.android.support:design:23.2.0'
在选项卡之间切换时,我开始出现此错误(Android版本5.0.2):
看起来像是一个支持库错误,有人也有这个问题吗?有什么解决方法吗?检查库中所有可能覆盖某些资源的“void android.graphics.drawable.drawable.setAlpha(int)”选项。void setAlpha(null)我没有看到这个问题,但是如果它确实在SwipeRefreshLayout小部件中,那么您可以对它进行子类化,并用try/catch块包装OnDetailedFromWindow调用
public class MySwipeRefreshLayout extends SwipeRefreshLayout {
// constructor methods here
@Override
public void onDetachedFromWindow() {
try {
super.onDetachedFromWindow();
catch (Exception ignore) {
}
}
}
在代码的以下部分引发异常:
private void setColorViewAlpha(int targetAlpha) {
mCircleView.getBackground().setAlpha(targetAlpha);
mProgress.setAlpha(targetAlpha);
}
这里的mCircleView(CircleImageView)是ImageView的一个实例。如果您在应用程序的任何位置执行任何释放内存的操作,请执行以下操作:
public void unbindDrawables(View view) {
try {
if (view != null) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ImageView) {
ImageView imageView = (ImageView) view;
if (imageView != null) {
Drawable drawable = ((ImageView) view).getDrawable();
if (drawable != null && drawable instanceof BitmapDrawable) {
drawable.setCallback(null);
}
imageView.setBackgroundResource(0);
imageView.setImageDrawable(null);
imageView.setBackgroundDrawable(null);
imageView.setImageBitmap(null);
}
} else if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
if ((view != null) && !(view instanceof AdapterView)) {
((ViewGroup) view).removeAllViews();
}
}
view = null;
}
} catch (Exception e) {
//TODO:: handle Exception
}
}
这些部件使mCircleView和它都是可绘制的NULL,当SwipeRefreshLayout调用重置时,就会抛出异常。调用得好!不是真正的问题,但它把我推向了正确的方向。我使用了一种实用的方法,将ImageView绘图和background绘图设置为null,希望能更快地释放内存。我也面临着同样的问题。你找到解决办法了吗?提前感谢。在我的情况下,挫折是可以理解的(空);导致问题的原因是:
public void unbindDrawables(View view) {
try {
if (view != null) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ImageView) {
ImageView imageView = (ImageView) view;
if (imageView != null) {
Drawable drawable = ((ImageView) view).getDrawable();
if (drawable != null && drawable instanceof BitmapDrawable) {
drawable.setCallback(null);
}
imageView.setBackgroundResource(0);
imageView.setImageDrawable(null);
imageView.setBackgroundDrawable(null);
imageView.setImageBitmap(null);
}
} else if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
if ((view != null) && !(view instanceof AdapterView)) {
((ViewGroup) view).removeAllViews();
}
}
view = null;
}
} catch (Exception e) {
//TODO:: handle Exception
}
}
if (view instanceof ImageView) {
ImageView imageView = (ImageView) view;
if (imageView != null) {
Drawable drawable = ((ImageView) view).getDrawable();
if (drawable != null && drawable instanceof BitmapDrawable) {
drawable.setCallback(null);
}
imageView.setBackgroundResource(0);
imageView.setImageDrawable(null);
imageView.setBackgroundDrawable(null);
imageView.setImageBitmap(null);
}
}