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 8091985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:11:28+00:00 2026-06-05T20:11:28+00:00

I am a beginner in both Java and Android, and I think this is

  • 0

I am a beginner in both Java and Android, and I think this is more of a Java question as it manifests in Android.

I am using an Android support package (android.support.v4.app) and creating a dialog using DialogFragment in my base class called MyActivity, which extends FragmentActivity.

The question I have concerns calling a function in the MyActivity class from an OnClick method in an OnClickListener method for a button in the DialogFragment.

It is working; I just want to understand why.

If I try to reference the function directly (MyActivity.someFunction()) I get “Cannot make a static reference to the non-static method someFunction() from the type MyActivity.” Anybody have a good way to explain static vs. non-static and why this particular reference is static? I assume it’s because the DialogFragment is declared as static. What is the purpose of declaring a subclass/method static vs. non-static. That is, why does it matter if the method belongs to the class and not the instantiated object?

Also, why and how does the casting get around the static reference in this example?

Thanks!

public static class myDialogFragment extends DialogFragment {
    static myDialogFragment newInstance(int whichDialog) {
        myDialogFragment f = new myDialogFragment();
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.invasive_edit_dialog, container, true);

        Button btn_apply_coords = (Button)v.findViewById(R.id.btn_get_coord);
        btn_apply_coords.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                    // This does not work ("Cannot make a static reference to the non-static method someFunction() from the type MyActivity").
                MyActivity.someFunction();


                    // This does not work ("The method someFunction() is undefined for the type FragmentActivity"). Eclipse suggests casting (a few lines down).
                getActivity().someFunction();

                    // This works; casted version of code above.  What is this code doing?
                ((MyActivity) getActivity()).someFunction();

                    // this works also
                MyActivity thisActivity =  (MyActivity) getActivity();
                thisActivity.someFunction();
            }
        });

        return v;
    }
}

public void someFunction() {
    // do something
}
  • 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-05T20:11:30+00:00Added an answer on June 5, 2026 at 8:11 pm
     // This does not work ("Cannot make a static reference to the non-static method someFunction() from the type MyActivity").
                MyActivity.someFunction();
    

    this didn’t work because you tried to call this method from class(MyActivity), not from object of that class (e.g. (MyActivity activity))

     // This works; casted version of code above.  What is this code doing?
                ((MyActivity) getActivity()).someFunction();
    

    and this did work, because, as I suppose, method getActivity() returned object, that was casted to MyActivity, and then non-static method from MyActivity was invoked on that object

    To sum up – you can’t call non-static methods without having object that you can call them on.

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

Sidebar

Related Questions

I am a beginner in both java and android. I am using asynchronous task
hello i'm a beginner java developer and this is one question in a list
This is a total beginner question, I've spent the past hour searching both stackoverflow
I am a total beginner in both simulations so this question might be silly.
Beginner in Android development. My code crashes. I have made a simple Java method
I'm an absolute beginner when it comes to using both SWIG and lua, and
A beginner question: I don't know how best to structure this bit of code,
I'm a beginner in Android. Here I have this code: public class GPS extends
I am new to both JAVA and Google App Engine. My Interest in Java
I'm new to both ajax and jquery, so please excuse the beginner question. I'm

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.