O_Casper_ne 2013-04-25 03:01 采纳率: 7.7%
浏览 2974

如何从静态方法中调用非静态方法?

我想从静态方法中调用非静态方法:

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
    }

我可以调用 Second_function 但是不能获得 Toast 和 CallCustomBaseAdapter() 方法,然后就出现崩溃。
如何处理这个问题呢?

  • 写回答

1条回答 默认 最新

  • franzhong 2013-04-25 04:28
    关注

    CallingCustomBaseAdapters()是类外函数吗,注意作用域,错误提示应该说的清楚
    getApplicationContext()前面默认有activity的this,你这样this变味了
    你可改为这个,Toast就出来了

    public void Second_function(Context ct)
        {
            Toast.makeText(ct,"Hello",1).show();
        }
    
    
    //.......
    SMS sms = new SMS();
    Context ct = getApplicationContext();
    sms.Second_function(ct);
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!