Android:AdapterView不支持addView(View) [英] Android: addView(View) is not supported in AdapterView

查看:609
本文介绍了Android:AdapterView不支持addView(View)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PreferenceFragment,它显示了一个动态的首选项列表,像这样实现;

I've got a PreferenceFragment that displays a dynamic list of preferences, implemented like this;

public class ConfigFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       addPreferencesFromResource(R.xml.settings);

       for(int i = 0; i<items.size();i++) {

          SettingsScreen s = new SettingsScreen(getActivity());

          PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(getActivity());
          screen.setTitle(itemName);
          screen.addPreference(s);
          indicatorCategory.addPreference(screen);
      }
}

SettingsScreen从首选项"派生并显示自定义布局;

SettingsScreen is derived from Preference and shows a custom layout;

public class SettingsScreen extends Preference {

@Override
protected View onCreateView(ViewGroup group) {

    LayoutInflater li = (LayoutInflater)getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );

    View v = li.inflate(R.layout.indicator_config, group, false);

    return v;
}

到目前为止,效果还不错.我现在面临的问题是尝试将视图动态添加到SettingsScreen时的问题. 以下内容将导致标题中所述的崩溃(AdapterView不支持UnsupportedOperationException:addView(View));

That works fine thus far. The issue I'm facing now is the when I try to dynamically add views to the SettingsScreen. The following would result in the crash stated in the title (UnsupportedOperationException: addView(View) is not supported in AdapterView);

Button b = new Button(getContext);
group.addView(b);

那么如何动态地将视图添加到SettingsScreen?

So how can I dynamically add Views to the SettingsScreen?

推荐答案

我想在stackoverflow上Android没什么大不了的.

Not much going on with Android here on stackoverflow I guess...

无论如何,回答我自己的问题;诀窍不是直接在视图中添加子视图,而是在视图的布局中添加子视图;

Anyway, to answer my own question; the trick is not to add subviews to the view directly, but rather to the view's layout;

View v = li.inflate(R.layout.indicator_config, group, false);
LinearLayout r = (LinearLayout) v.findViewById(R.id.configLayout);
Button b = new Button(getContext);
r.addView(b);

这篇关于Android:AdapterView不支持addView(View)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆