3

I am facing a big problem in calling non static method from static method.

This is my code

Class SMS
{
    public static void First_function()
    {
        SMS sms = new SMS();
        sms.Second_function();
    }

    public void Second_function()
    {
        Toast.makeText(getApplicationContext(),"Hello",1).show(); // This i anable to display and cause crash
        CallingCustomBaseAdapters();    //this was the adapter class and i anable to call this also
    }

I am able to call Second_function but unable to get Toast and CallCustomBaseAdapter() method, crash occurs.

What should I do to fix that issue ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Vishnu
  • 349
  • 3
  • 8
  • 16
  • Try to pass the context as a argument in the non-static method using static method. I means pass the context to static method and then pass same context to non-static method. – Dharmendra Oct 09 '12 at 10:02
  • I think besides acquainting yourself with some Java code standards (e.g. capitalization) you should ask what are you trying to do. I'm not even sure how your code possibly compiles - SMS does not seem to have access to any context. SMS should extend some class that derives from Context. Your code would work fine if there was nothing inside the second method but there is stuff that isn't legal. Also, no one uses 1 as a Toast length, use Toast.LENGTH_{SHORT|LONG} – aamit915 Oct 09 '12 at 10:02
  • There is no way to achieve your goal and it would be weird if there was an overcome. `static` methods and fields belong to classes and non-`static` methods and fields belong to class instances. To call a non-`static` method you need an instance of a class, and that's all. Hope this helps. – Egor Oct 09 '12 at 09:56
  • The member functions are being called on a specific instance. – aamit915 Oct 09 '12 at 09:59
  • @ Egor , yeah i agree with you, here i used Class instance to call non static method, and from Second_function(non static method) i want to call another method and custom adapters, but i cant.. how to do this? – Vishnu Oct 09 '12 at 09:59

2 Answers2

8
  public static void First_function(Context context)
  {
    SMS sms = new SMS();
    sms.Second_function(context);
  }

  public void Second_function(Context context)
  {
    Toast.makeText(context,"Hello",1).show(); // This i anable to display and cause crash
  }

The only solution to achieve this is that you need to pass the current context as a parameter. I wrote the code for only Toast but you need to modify it as per your requirements.

pass the Context from the your activity First_function(getApplicationContext()) etc..

for static string

public static String staticString = "xyz";

public static String getStaticString()
{
  return staticString;
}


String xyz = getStaticString();
V.J.
  • 9,492
  • 4
  • 33
  • 49
1

You should have a reference to a Context. You are trying to get the application context from inside a SMS instance.

I guess you are calling the First_function from an Activity or Service. So you can do this:

Class SMS
{
    public static void First_function(Context context)
    {
        SMS sms = new SMS();
        sms.Second_function(context);
    }

    public void Second_function(Context context)
    {
        Toast.makeText(context,"Hello",1).show(); // This i anable to display and cause crash
        CallingCustomBaseAdapters();    //this was the adapter class and i anable to call this also
    }

Then, from your activity:

SMS.First_function(this); //or this.getApplicationContext() 
Sebastian Breit
  • 6,137
  • 1
  • 35
  • 53
  • Can u tell me how to have a reference? – Vishnu Oct 09 '12 at 09:56
  • How is your code ANY better then the code that was originally written? SMS still has no reference to a context - it's just a class that doesn't extend activity. The original code was fine in the sense that non-static methods were not being called in a static context. Unfortunately, it seems Vishnu has no idea what's going on but that doesn't mean you just go along with it. – aamit915 Oct 09 '12 at 10:05
  • You'll have to post all your code if you want any help from me. I'm sorry but unfortunately I cannot understand what you're trying to do :( – aamit915 Oct 09 '12 at 10:18
  • lol ...stackoverflow users are funny sometimes.. the accepted answer is exactly the same as mine, but mine has one downvote.. excelent – Sebastian Breit Oct 09 '12 at 10:54