引起:java.lang.UnsupportedOperationException:AdapterView不支持addView(View)

时间:2016-11-07 14:12:37

标签: android android-studio

在陈述我的问题之前,我看到了与同一错误sort相关的类似问题。但它似乎没有帮助。

我有在Settings菜单上按下设置时调用的SettingsActivity。

Task.delay

这是res / layout / activity_settings.xml

    public class SettingsActivity extends AppCompatActivity{

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

        Log.v("Settings:OnCreate","Going to set View in SettingsActivity");
        setContentView(R.layout.activity_settings);
        Log.v("Settings:OnCreate","View is set in SettingsActivity");

      //When I don't add the below part, activity_settings is displayed properly.
      //I am getting the error when this below part is added
        if(savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .replace(R.id.activity_settings, new MyPreferenceFragment())
                    .commit();
        }
    }



    public static class MyPreferenceFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v("Fragment:OnCreate","Going to add preferences");
        addPreferencesFromResource(R.xml.pref_general);
        bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
        Log.v("Fragment:OnCreate","Added preferences");
    }

    private void bindPreferenceSummaryToValue(Preference preference) {
        // Set the listener to watch for value changes.
        preference.setOnPreferenceChangeListener(this);

        // Trigger the listener immediately with the preference's
        // current value.
        onPreferenceChange(preference,
                PreferenceManager
                        .getDefaultSharedPreferences(preference.getContext())
                        .getString(preference.getKey(), ""));
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();

        if (preference instanceof ListPreference) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list (since they have separate labels/values).
            ListPreference listPreference = (ListPreference) preference;
            int prefIndex = listPreference.findIndexOfValue(stringValue);
            if (prefIndex >= 0) {
                preference.setSummary(listPreference.getEntries()[prefIndex]);
            }
        } else {
            // For other preferences, set the summary to the value's simple string representation.
            preference.setSummary(stringValue);
        }
        return true;
    }

}
}

这是~res / xml / pref_general.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_settings"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

</ListView>

当我包含MyPreferenceFragment()时,我收到错误。我阅读了有关使用this sentence is added的文档,我并不认为我在其实施中犯了错误。请帮我解决这个问题。

编辑:我正在添加崩溃堆栈

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent">

    <EditTextPreference
        android:title= "@string/pref_location_label"
        android:key="@string/pref_location_key"
        android:defaultValue="@string/pref_location_default"
        android:inputType="text"
        android:singleLine="true"   />

</PreferenceScreen>

1 个答案:

答案 0 :(得分:1)

删除你的布局。然后,替换:

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

    Log.v("Settings:OnCreate","Going to set View in SettingsActivity");
    setContentView(R.layout.activity_settings);
    Log.v("Settings:OnCreate","View is set in SettingsActivity");

  //When I don't add the below part, activity_settings is displayed properly.
  //I am getting the error when this below part is added
    if(savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .replace(R.id.activity_settings, new MyPreferenceFragment())
                .commit();
    }
}

使用:

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

    Log.v("Settings:OnCreate","Going to set View in SettingsActivity");
    Log.v("Settings:OnCreate","View is set in SettingsActivity");

  //When I don't add the below part, activity_settings is displayed properly.
  //I am getting the error when this below part is added
    if(savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new MyPreferenceFragment())
                .commit();
    }
}

或者,您可以保留布局和现有Java代码,但将布局中的activity_settings窗口小部件更改为FrameLayout而不是ListView。这样运行得慢一些并且使用更多的内存,但它会起作用。

PreferenceFragment提供了自己的ListView。你不需要一个。