title: Android Imagebutton change Image OnClick
body: <p>I just added a new <code>drawable</code> folder under <code>res</code> folder. In the <code>drawable</code> folder, i copied the <code>ic_launcher.png</code> file from <code>drawable-hdpi</code> folder.
  I wanna change the standard <code>ImageButton</code> image through the new one when i press the button. I wrote some code, but when i start the app, it crashes.</p>
  
  <pre><code>Button imgButton; 
  
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      findViewById(R.id.imgButton).setOnClickListener(imgButtonHandler);      
  }
  
  View.OnClickListener imgButtonHandler = new View.OnClickListener() {
  
      public void onClick(View v) {
  
          imgButton.setBackgroundResource(R.drawable.ic_launcher);
  
      }
  };
  </code></pre>
  
  <p>EDIT: I changed to this, and this also not works.</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      imgButton = (Button) findViewById(R.id.imgButton);
      imgButton.setOnClickListener(imgButtonHandler);
  }
  
  
  View.OnClickListener imgButtonHandler = new View.OnClickListener() {
  
      public void onClick(View v) {
          imgButton.setBackgroundResource(R.drawable.ic_launcher);
  
      }
  };
  </code></pre>
  
  <p>EDIT 2: THIS WORKS. Thanks to all.</p>
  
  <pre><code>ImageButton button;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      button= (ImageButton)findViewById(R.id.imgButton);
      button.setOnClickListener(imgButtonHandler);
  }
  
  
  View.OnClickListener imgButtonHandler = new View.OnClickListener() {
  
      public void onClick(View v) {
          button.setBackgroundResource(R.drawable.ic_launcher);
  
      }
  };
  </code></pre>