0

Suppose I have the xml:

<LinearLayout ...> 
  <LinearLayout a:id=«@+id/inner» .../> 
</LinearLayout>

And what I am writing in code:

LinearLayout inner = (LinearLayout) findViewById(R.id.inner);
RelativeLayout.LayoutParams params = ((RelativeLayout.LayoutParams)) inner.getLayoutParams();

This is definitely an error which leads to exception in runtime, because the parent of inner is not RelativeLayout, but LinearLayout, and their layoutparams can not be cast to each other.

Is there any static analyzer or a lint rule to verify that? How do we eliminate that kind of erros?

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • if i follow your question.. may be.. the `instanceof` can do the trick for `LinearLayout` or `RelativeLayout.LayoutParams`. and if you are casting it as .. `(LinearLayout) findViewById` than you should definetly know the type of view. – SRB Bans Nov 18 '16 at 09:44
  • The instanceof check is will just return false and it prevents crash, but the error still persist. We obtain layoutparams not out of pure curiosity, but to change its values. – Vladimir Ivanov Nov 18 '16 at 09:48

0 Answers0