HarmonyOS鸿蒙Next中getContext(this)能否在自定义类中使用
HarmonyOS鸿蒙Next中getContext(this)能否在自定义类中使用?
4 回复
getContext需要在组件内调用,要在自定义的类里用的话,需要确保自定义类的调用是在组件里。
更多关于HarmonyOS鸿蒙Next中getContext(this)能否在自定义类中使用的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
只要是ets就可以
不行,一直获取不到都是undefined。
在HarmonyOS鸿蒙Next中,getContext(this)
通常用于获取当前Activity或Service的上下文。在自定义类中,如果该类不直接继承自Activity或Service,无法直接使用getContext(this)
。你可以通过构造函数或方法参数将上下文传递给自定义类。例如:
public class MyCustomClass {
private Context context;
public MyCustomClass(Context context) {
this.context = context;
}
// 使用context进行相关操作
}