title: Android: App crashes on calling toast from another class
body: <p>I am a novice Android Developer.
  I have created a <strong>package-private class</strong> which extends Application, and contains the required code for specific functions. I basically want to display if the user-selected button is the correct choice or not, via a toast. Since I have to call this code for many activities, I just created a package-private class for it. However, on clicking the button, the app crashes. Please see the code given below for reference.</p>
  
  <blockquote>
    <p><em>I cannot change the onClick method to non-static because if I do that, Android Studio shows an error, and if I change it to static, I am unable to use the method <code>getApplicationContext()</code>, because it is not accessible inside static blocks.</em></p>
  </blockquote>
  
  <p>I think that using <code>view.getContext()</code> is causing the crash.</p>
  
  <p><strong>Is there any workaround, or a solution?</strong>
  Your help would be greatly appreciated. Thanks :)</p>
  
  <p>Here is the code for your reference.</p>
  
  <blockquote>
    <p>activity.java:</p>
  </blockquote>
  
  <pre><code>public class activity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          Button btn = (Button) findViewById(R.id.btn);
          btn.setOnClickListener(functions.select);
          functions.makeLayout(expression, buttons);
      }
  }
  </code></pre>
  
  <p>Here is the code which crashes the app. </p>
  
  <blockquote>
    <p>functions.java:</p>
  </blockquote>
  
  <pre><code>class functions extends Application {
  
      private static int idx;
  
      public static View.OnClickListener select=new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              int selected_index=(int) view.getTag();
              if(selected_index==idx)
              {
                  Toast.makeText(view.getContext(), "Correct.", Toast.LENGTH_LONG).show();
                  ((Button) view).setTextColor(Color.GREEN);
              }
              else
              {
                  Toast.makeText(view.getContext(), "Wrong.", Toast.LENGTH_LONG).show();
                  ((Button) view).setTextColor(Color.RED);
              }
          }
      };
  </code></pre>