Android 日常bug集锦

  1. 今天开发的时候遇到了一个bug,一直报xml inflate异常,我检查了好多遍就是没问题,后来才发现是资源文件格式的问题,好神奇的bug。就是图片格式后面带V21的,查阅资料才知道,v21只能在api21以上使用,否则会报xml布局错误,我的解决方法是把图片再copy 覆盖一下,就会自动生成不是v21的图片,bug完美解决。
  2. ScrollView中放ViewPager时,有时候不显示,原因是这个时候的测量模式是UNSPECIFIED
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // For simple implementation, our internal size is always 0.
    // We depend on the container to specify the layout size of
    // our view. We can't really know what it is since we will be
    // adding and removing different arbitrary views and do not
    // want the layout to change as this happens.
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec),
    getDefaultSize(0, heightMeasureSpec));
    }
    public static int getDefaultSize(int size, int measureSpec) {
    int result = size;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);
    switch (specMode) {
    case MeasureSpec.UNSPECIFIED:
    result = size;
    break;
    case MeasureSpec.AT_MOST:
    case MeasureSpec.EXACTLY:
    result = specSize;
    break;
    }
    return result;
    }

而UNSPECIFIED这个时候返回的大小是0,所以不显示
3.有时候程序出现”cannot be resolved or is not a field”这个错误,有可能是复制项目的时候,把R文件复制过来了,删除错误的R文件,重新Build,搞定!
4.ToolBar的setTitle无效,把它放在setSupportActionBar之前就ok,但是setNavigationOnClickListener要放在setSupportActionBar之年才会调用。
5.java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/animation/AnimatorCompatHelper;
design 的包版本不一致