android编程会有很多警告, 一眼望去, 这个代码黄黄的一片, 很多人说, 不要管.

可是, 这其中某些确实是我们要管的. 可以让代码更清晰, 更有效率, 避免很多隐藏的bug.

可是其中, 又有一些bug是我们不要管的. 咋办? 咱们具体问题具体分析吧.

第一件事: 如何看这些警告

  1. 怎么看, 目前的悬停, f1方法, 太令人郁闷了.
  2. 用页面右边的黄颜色看.
  3. 代码分析的结果也可以看,
    • 这个结果不是分页而是分类的.
    • 如果喜欢这种方式, 可以: code inspect.

1. 这个警告有深度: getLayoutInflater().inflate(R.layout.activity_main, null); 第二个参数不可以为null

//在mainactivity里面声明一个属性.
private   View layoutmain;
//在onactivity里面这样setcontent, 这样做的原因是, 可以保留这个layoutmain, 下次back回主页, 要重新setcontent时, 可以直接用, 避免内存泄露. 
layoutmain = getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(layoutmain);
  • 我知道, 这个地方其实有root的.
  • 这个root就是id为content的那个flamelayout.
  • 但是, 如何弄到这个content
  • 为了解决这个warning, 我需要深入阅读activity的源代码. 这个是解决问题的王道, 看一个小时的源码, 比啥都强.

在big nerd ranch提问 — 三天过去了, 也没有答复, 不推荐这种做法.

  • good article! and i have a question to ask, beg your answer.

    when i use this in a activity’s oncreate() .like:

    View layoutmain = getLayoutInflater().inflate(R.layout.activity_main, null);
    setContentView(layoutmain);
    

    what can i use two replace the null?
    thank you:)

  • https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/

    • 用machangkun@gmail.com登录
  • https://forums.bignerdranch.com/t/when-i-inflate-a-view-for-activitys-setcontentview/9171

    • 用307012389登录

万能的stackoverflow:

http://stackoverflow.com/questions/26404951/avoid-passing-null-as-the-view-root-warning-when-inflating-view-for-use-by-ale

  1. 方案一:

    View layoutmain = View.inflate(this, R.layout.activity_main, null); 
    //根本不用layoutinflater.inflater, 改用view.inflater就可以了.
    
  2. 方案二:

    The short story is that when you are inflating a view for a dialog, parent should be null, since it is not known at View inflation time. In this case, you have three basic solutions to avoid the warning:

    Suppress the warning using an @Suppress
    Inflate the View using View’s inflate method. This is just a wrapper around a LayoutInflater, and mostly just obfuscates the problem.

  3. 方案三:

    View dialogView = li.inflate(R.layout.input_layout,(ViewGroup)null);
    

有了新的方案: http://www.jianshu.com/p/3b4dc52fbae4/comments/1310140

工匠弱水: http://www.devtf.cn/?p=560

从源码角度分析

  1. 貌似 this.getParent(); 有戏.

  2. 跟踪代码, 得到这一句:(ViewGroup) mSubDecor.findViewById(android.R.id.content);

  3. 在setcontent的源代码private ViewGroup createSubDecor()里面发现了这个: (ViewGroup) inflater.inflate(R.layout.abc_screen_simple, null);

  4. 上面这个函数最后: abcContent.setId(android.R.id.content); 因此在setcontent之前, 我弄不到这个id.

  5. android的源代码已经变了, 但是, 依旧是傻傻的.

  6. 用log跟踪代码:

    不需要在源码里改,在其他地方 Log.d(“TAG”, getWindow().getClass().getName()); 看看 getWindow() 返回个啥就可以了。

2, 这个警告有数量: final RelativeLayout.LayoutParams themerl = (RelativeLayout.LayoutParams) iv2.getLayoutParams(); 这个地方会有, nullpointerexception, 因为iv2可能是null.

ImageView iv2 = (ImageView) findViewById(R.id.image2);
final RelativeLayout.LayoutParams themerl = (RelativeLayout.LayoutParams) iv2.getLayoutParams();
  1. 使用断言
    assert iv2!=null;
    
  2. 使用@Nullable and @NotNull. //这个我没有成功. 貌似用在声明的地方, http://stackoverflow.com/questions/271526/avoiding-null-statements

  3. person?.getAddress()?.getPostcode(); ?代表不能为null. 但是, 这个提案没有被实现.

  4. 使用泛型, functional等等解决. 还是参考上面这个文档.

  5. 使用spring或者apache的基础类库. 或者google guava类库.

3, 警告竟然就这样清空了!!!

我没有碰到其他的有趣的warning, 都比较好解决.

或许说, 很多warning都真的是代码问题, 需要重构代码解决.