john smith john smith - 26 days ago 18
Android Question

Caused by: java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView

Before stating my problem, I saw similar question related to same error here. But it doesn't seem to help.

I have the SettingsActivity which gets called when Settings is pressed on Menu.

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;
}

}
}


Here is the res/layout/activity_settings.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>


Here is the ~res/xml/pref_general.xml

<?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>


I am getting the error when I included the MyPreferenceFragment(). I read the documentation on using PreferenceFragment and I don't think that I made mistakes in it's implementation. Please help me out in solving this issue.

Edit: I am adding the crash stack

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.home.sunshine, PID: 19190
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.home.sunshine/com.example.home.sunshine.SettingsActivity}: java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
at android.widget.AdapterView.addView(AdapterView.java:474)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:990)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1153)
at android.app.BackStackRecord.run(BackStackRecord.java:800)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
at android.app.FragmentController.execPendingActions(FragmentController.java:325)
at android.app.Activity.performStart(Activity.java:6619)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2542)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:207) 
at android.app.ActivityThread.main(ActivityThread.java:5776) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 

Answer

Delete your layout. Then, replace:

@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();
    }
}

with:

@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();
    }
}

Alternatively, you can keep your layout and existing Java code, but change the activity_settings widget in the layout to be a FrameLayout instead of a ListView. This runs a bit more slowly and uses a bit more memory, but it will work.

A PreferenceFragment provides its own ListView. You do not need one.

Comments