title: initialized imageview in onCreate but still null
body: <p>I want to achieve the following: Once user click a button, change current layout(main) to the other and the other layout has an image loaded. But I keep getting error message say "virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference". The following is my mainactivity.java and 'the other layout'.xml</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  
      private Button mybtn;
      private ImageView myimage;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          init();
      }
  
      public void init(){
  
          myimage = (ImageView) findViewById(R.id.imgview);
          mybtn = (Button) findViewById(R.id.btn);
          mybtn.setOnClickListener(new MyListener());
      }
  
      public class MyListener implements View.OnClickListener {
  
          @Override
          public void onClick(View v){
              switch (v.getId()){
  
                  case R.id.btn:
                      setContentView(R.layout.the_other_layout);
                      initMap();
              }
          }
      }
  
      public void initMap(){
  
          DisplayMetrics dm = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(dm);
          BitmapFactory.Options options = new BitmapFactory.Options();
          options.inScaled = false;
          Bitmap bmp = null;
          bmp = BitmapFactory.decodeResource(getResources(),R.drawable.somepngfile,options);
          myimage.setImageBitmap(bmp);
      }
  }
  </code></pre>
  
  <p>The follwoing is the_other_layout xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;ImageView
          android:id="@+id/imgview"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="horizontal"
          /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>Please check the answer below if you have the similar problem</p>