Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8336037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:51:49+00:00 2026-06-09T03:51:49+00:00

I have two activites called Activity_A and Activity_B . I have a Method or

  • 0

I have two activites called Activity_A and Activity_B. I have a Method or Function in Activity_A like:

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

Now I want to call this Method from my another activity called Activity_B.

I am trying to call this Method using following lines:

((Activity_A) this.getApplicationContext()).printNumber();

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

java.lang.ClassCastException: android.app.Application cannot be cast to com.example.app.Activity_A

How can I do this?

Edit: I found this question and acording to “Rich” extends Activity_A to Activity_B. But the problem is Activity_B is a list activity and I have already extends this with ListActivity.

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:51:50+00:00Added an answer on June 9, 2026 at 3:51 am

    getApplicationContext will give you a class of type Application not Activity. So this wont work, You will have to store a static context of activity and then call this method. If you post details of what you are trying to do then someone will suggest solutions

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one class called Global and two other activities. In each activity I
Say we have two activities, Activity1 and Activity2. In Activity1's onClick() method, we have
I have a method called getData() in an Activity that returns a String[]: Example:
I have two activity: MediaPlayer_Activity (display the control like play,pause,stop,next....) MediaPlayerPlayList_Acitivty (to display all
Sorry for my bad English. I have two activity. called Act1 and Act2 Act2
I have two Activities along with one Application class, and whenever I call my
I have two activities. In first I come to the second activity from first
If I have two activities in my app, one with listview and the other
This is my situation. I have two activities: ONE and TWO. In TWO activity
I am experiencing a little pain on Android. I have two activities, one is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.