title: Android Dialog NoSuchMethodException error when using XML onClick
body: <p>I'm new to Java and Android, and I'm working on my first test app.</p>
  
  <p>I've progressed with it, but I'm blocked with a Dialog.</p>
  
  <p>I show the dialog from the Activity like this:</p>
  
  <pre><code>//BuyActivity.java
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_shop);
  
      initialize_PR();
      display_PR();
      BuyDialog=new Dialog(this);
      BuyDialog.setContentView(R.layout.dialog_buy);
  
  }
  public void Action_ShowDialog_Buy(View view) {
      BuyDialog.show() ;
  }
  </code></pre>
  
  <p>And the dialog is correctly shown when the button of the Activity that triggers Action_ShowDialog_Buy is clicked. But after that, the Dialog itself has a button:</p>
  
  <pre><code>&lt;!-- dialog_buy.xml --&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" &gt;
  
  &lt;!-- Other stuff --&gt;
  
  &lt;Button
      android:id="@+id/Button_Buy"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/Some_Other_Stuff"
      android:layout_centerHorizontal="true"
      android:text="@string/button_buy"
      android:onClick="Action_ShowDialog_Buy" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>The button method Action_ShowDialog_Buy is implemented on the Activity:</p>
  
  <pre><code>public void Action_ShowDialog_Buy(View view) {
      BuyDialog.dismiss() ;
  }
  </code></pre>
  
  <p>but when I click on the Button in the Dialog I receive the error:</p>
  
  <pre><code>java.lang.IllegalStateException: Could not find a method BuyActivity.Action_ShowDialog_Buy(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'Button_Buy'
  </code></pre>
  
  <p>and below:</p>
  
  <pre><code>Caused by: java.lang.NoSuchMethodException:BuyActivity.Action_ShowDialog_Buy
  </code></pre>
  
  <p>but as you can see above, the method exists on the Activity.</p>
  
  <p>I think I understand this is some kind of scope issue, but I do not manage to understand it. Please note that I have readed <a href="https://stackoverflow.com/questions/4243704/using-onclick-attribute-in-layout-xml-causes-a-nosuchmethodexception-in-android">Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs</a> but I need to understand, not just to copy code.</p>
  
  <p>Many thanks</p>