3

I tried to call non static method from static method but without any result ,my application works crash my code :

public class MainActivity extends Activity  {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        setAuth();
        ///

        ///



    }
    public static void setAuth() {

                new MainActivity().d();
        }
    public void d()
    {

        Toast.makeText(getApplicationContext(), "fff",Toast.LENGTH_SHORT).show();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }



    }

Is it permissible to call non static method from static method in android?? and how???

4
  • 1
    post our crash error log ! Commented Nov 20, 2013 at 7:09
  • First off all you can't do new MainActivity() for an Activity. I think your main task is to show Toast. So pass a Context object as param to setAuth() and show your toast using that context object. Commented Nov 20, 2013 at 7:10
  • 1
    You should consider spending a bit more time on simple Java programming before trying to make an Android app. Commented Nov 20, 2013 at 7:15
  • code isn't from my application, I tried simple code ,if the code work then change my application Commented Nov 20, 2013 at 7:21

3 Answers 3

2

A static method in a class must be able to be executed without any reference to an instantiation of the class:

class MyClass {
    int information;
    static int usefulNumber = 72;

    int method() {
        return information;
    }

    static int methodStatic() {
        // Cannot refer to information
        // But can refer to usefulNumber
    }
}

By definition therefore, it cannot execute a non-static method in the class because that method doesn't exist unless, as @RhinoFeeder says, you have instantiated the class and passed that instantiation to the static class:

    static int methodStatic2(MyClass myClass) {
        return myClass.method();
    }
Sign up to request clarification or add additional context in comments.

Comments

0

The only way to do this is if you have access to an instance of the class that contains the non-static method.

EDIT: I realized this answer sounds empty without a further explanation, since you do new-up a MainActivity.

new MainActivity().d();

Will not work in Android, as you cannot create a new Activity that way.

1 Comment

Honestly, I would abandon this design all together. I would make setAuth take in a Context, and use that context to create the Toast in that method, axing d() all together. If you are set on the design you have now, @NeilTownsend provided a solid answer.
0
public static void setAuth(MainActivity activity) {
       activity.d();
}

That simple.

new MainActivity().d(); calls the method of another instance of the activity.

1 Comment

Ok, try to call d() method from onCreate method and remove the call of the static method. to check if that's what is really causing the crash. because I don't believe it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.