how to call non static method from static method in android

六眼飞鱼酱① 提交于 2019-12-06 01:34:34
  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();

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() 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!