title: Android RelativeLayout.getHeight returns 0; research done but the popular solution is giving me error
body: <p>Edit: I'm trying to get the height/width of a view, so that I can programmatically put some <code>ImageView</code> into it. However  <code>getHeight()</code> and  <code>getWidth()</code> returns 0 so I was not able to put the <code>ImageView</code> into the correct positions. I did make the <code>ImageView</code> appear on the <code>RelativeView</code> (<code>main_activity</code>) though.</p>
  
  <p>I'm aware of the answer related the <code>getHeight()</code> returning <code>0</code> error: <a href="https://stackoverflow.com/questions/4142090/how-to-retrieve-the-dimensions-of-a-view/4406090#4406090">How to retrieve the dimensions of a view?</a> <br>Unfortunately it's gives me exception on the line with <code>ld.setLayerInset()</code>, saying I'm trying to invoke the method on a null pointer reference. I' aware of the meaning of such an exception, but I don't understand what's wrong with my code as I was just trying to copy and paste the codes on the above link to my codes with amendments on the variable names. Here are some of my codes:<br></p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      findViewById(R.id.activity_main).getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
          public void onGlobalLayout() {
              LayerDrawable ld = (LayerDrawable) findViewById(R.id.activity_main).getBackground();
              ld.setLayerInset(1,0,0,0,0);
              if (Build.VERSION.SDK_INT &gt;= 16) {
                  findViewById(R.id.activity_main).getViewTreeObserver().removeOnGlobalLayoutListener(this);
              } else {
                  findViewById(R.id.activity_main).getViewTreeObserver().removeGlobalOnLayoutListener(this);
              }
          }
      });
  ...
  }
  </code></pre>
  
  <p>I think the problem occurs on <code>getBackground()</code> getting <code>null</code> then cause the next line to throw exception, but I don't know what's the problem at all.</p>