0

在我的应用程序中,我使用 popupwindow 扩展了我的自定义视图。

现在里面有一个 AutoCompleteTextview 提供建议。

现在的问题是,每当我开始在 AutoCompleteTextView 中输入文本时,应用程序就会崩溃。

错误日志:-

Unable to add window -- token android.view.ViewRootImpl$W@1c6ea57 is not valid; is your activity running?

问题来了:-

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        View view = convertView;
        if (view == null) {
            if (inflater == null)
                inflater = LayoutInflater.from(parent.getContext());    // ISSUE
            view = inflater.inflate(R.layout.chips_adapter,parent, false);   // ISSUE
            vh = new ViewHolder();
            vh.tv = (TextView) view.findViewById(R.id.textView1);

            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }

        vh.tv.setText(suggestions.get(position).getTitle());

        return view;
    }

我认为错误是关于在另一个膨胀布局之上膨胀布局。

4

1 回答 1

0

在您的适配器类中:

使用上下文参数创建构造函数。

示例:(考虑类名是 AdapterClass)

Class AdapterClass {
   LayoutInflater inflater;
   private Context context;

   public AdapterClass(Context context){
        this.context = context;
        inflater = LayoutInflater.from(this.context);
   }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        View view = convertView;
        if (inflater == null){
            view = inflater.inflate(R.layout.chips_adapter,parent, false);
            vh = new ViewHolder();
            vh.tv = (TextView) view.findViewById(R.id.textView1);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }

        vh.tv.setText(suggestions.get(position).getTitle());

        return view;
    }
}

注意:- 当您调用 AdapterClass 时,不要忘记使用 Context 作为参数初始化构造函数。

于 2016-07-27T13:23:37.127 回答