问题描述
- 如何从静态方法中调用非静态方法?
- 我想从静态方法中调用非静态方法:
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() 方法,然后就出现崩溃。
如何处理这个问题呢?
解决方案
CallingCustomBaseAdapters()是类外函数吗注意作用域错误提示应该说的清楚
getApplicationContext()前面默认有activity的this你这样this变味了
你可改为这个,Toast就出来了
public void Second_function(Context ct) { Toast.makeText(ctHello""1).show(); }//.......SMS sms = new SMS();Context ct = getApplicationContext();sms.Second_function(ct);
时间: 2024-08-09 22:45:58