java– 如何从android中的静态方法调用非静态方法

java– 如何从android中的静态方法调用非静态方法,第1张

概述我在静态方法调用非静态方法时遇到了一个大问题.这是我的代码ClassSMS{publicstaticvoidFirst_function(){SMSsms=newSMS();sms.Second_function();}publicvoidSecond_function(){Toast.makeText(getApplica @H_419_0@我在静态方法中调用非静态方法时遇到了一个大问题.

@H_419_0@这是我的代码

@H_419_0@

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    }
@H_419_0@我能够调用Second_function但无法获取Toast和CallCustomBaseAdapter()方法,发生崩溃.

@H_419_0@我该怎么做才能解决这个问题?

解决方法:

@H_419_0@
  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  }
@H_419_0@实现此目的的唯一解决方案是您需要将当前上下文作为参数传递.
我只为Toast编写了代码,但您需要根据自己的要求对其进行修改.

@H_419_0@从您的活动First_function(getApplicationContext())等传递Context.

@H_419_0@对于静态字符串

@H_419_0@

public static String staticString = "xyz";public static String getStaticString(){  return staticString;}String xyz = getStaticString();

总结

以上是内存溢出为你收集整理的java – 如何从android中的静态方法调用非静态方法全部内容,希望文章能够帮你解决java – 如何从android中的静态方法调用非静态方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1115511.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存