android - 为什么在 Android ContentProvider 中我不能使用像 'this' 这样的上下文而不是 getContext()
问题描述
public boolean onCreate() {
Context context = getContext();
mDbHelper = new DbHelper(context);
return true;
}
public boolean onCreate() {
mDbHelper = new DbHelper(this); // why this not work?
return true;
}
为什么我不能使用像'this'这样的上下文而不是getContext()
解决方案
通常,您可以在 Activity 或 Application 中使用“this”,因为它们可以充当上下文。
如果您的类不是从 Context 或 Activity 或 Application 扩展而来,则不能使用“this”,因为“DbHelper”构造函数需要上下文。
推荐阅读
- timer - 计数器错误中的异步加载数据
- linux - 从本地复制的 repo 安装 pip
- python - 试图伪造和轮换用户代理
- flutter - Flutter - 如何在网络未连接时确定使用 Firestore
- angular - 如何从集合中获取一个文档,离子选择列出所有文档?
- audio - 使用 Julia 在声音上使用 fft
- java - 为什么我缓冲的 `String` 与 `RegEx` 模式不匹配?
- python - 如何修复'TypeError:'module'对象不可调用'
- mysql - 如果至少一个条件不满足,则消除所有标识符
- html - 如何重新调整容器大小以适合其子项的宽度