ClassCastException on calling a function from other activity
<p>I have two activites called <code>Activity_A</code> and <code>Activity_B</code>. I have a Method or Function in Activity_A like:</p>

<pre><code>public void printNumber (int i) {
    for(int j = 0; j &lt;= i; j++) {
        Log.w("TAG", "Print number is: " + j);
    }
}
</code></pre>

<p>Now I want to call this Method from my another activity called Activity_B.</p>

<p>I am trying to call this Method using following lines:</p>

<pre><code>((Activity_A) this.getApplicationContext()).printNumber();
</code></pre>

<p>I write this line in onCreate of Activity_B and this will crash my application and logcat shows this error:</p>

<pre><code>java.lang.ClassCastException: android.app.Application cannot be cast to com.example.app.Activity_A
</code></pre>

<p>How can I do this?</p>

<p><strong>Edit:</strong>  I found <a href="http://stackoverflow.com/q/5086446/1250370">this</a> question and acording to "Rich" extends <code>Activity_A</code> to <code>Activity_B</code>. But the problem is <code>Activity_B</code> is a list activity and I have already extends this with <code>ListActivity</code>.</p>

<p>and other answer create an instance of <code>Activity_A</code> and then call method. If I do this a all the variable's become empty or null of <code>Activity_A</code>. And I don't want to make a static method.</p>

<p>Is there any other way to do this without create a static reference or another instance?</p>
