试图从非可视上下文访问 UI 个常量

Tried to access UI constants from a non-visual Context

我 运行 我的图像查看器应用程序处于 StrictMode 状态,试图赶上内存泄漏。在显示许多缩略图的 activity 中,我多次看到错误(每个缩略图可能一次):

E/ViewConfiguration: Tried to access UI constants from a non-visual Context:android.app.Application@7f56ef7UI constants, such as display metrics or window metrics, must be accessed from Activity or other visual Context. Use an Activity or a Context created with Context#createWindowContext(int, Bundle), which are adjusted to the configuration and visual bounds of an area on screen
java.lang.IllegalArgumentException: Tried to access UI constants from a non-visual Context:android.app.Application@7f56ef7

此错误是在 ImageAdapter class 扩展 BaseAdapter:

getView 方法中触发的
    ....
    public View getView(int pos, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.thumbitem, parent, false);
            ...

这一行 convertView = inflater.inflate... 是给出错误的那一行。

class 有以下方法也会触发 Tried to access visual service LayoutInflater from a non-visual Context 错误:

ImageAdapter() {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    }

看来我的 inflater 语境有误?哪一个是正确的?

Which would be the correct one?

你的 Activity 是正确的 Context。理想情况下,在 Activity 上调用 getLayoutInflater() 并使用它。