3

I have several activities that use custom layouts for a preference activity. The style is as follows.

public class SettingsActivity extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
    //Configure toolsbar

    //more custom stuff using "findViewById"

    PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
    setPreferenceScreen(root);
    //Add preferences

}

The problem is that on Android 6.0, setContentView isn't working for me. findViewById returns null, where it returns the Toolbar on API 9-API22. Did this change? What is going on here?

Marlon
  • 1,839
  • 2
  • 19
  • 42
Justin
  • 3,322
  • 2
  • 22
  • 37
  • 1
    setContentView is not made for use with PreferenceActivity. The layout is handled for you. You just supply the preference XML or fragments with preference XML. – Eugen Pechanec Oct 31 '15 at 19:00
  • When something works flawlessly for 14 releases of an API, it is supported. Otherwise, PreferenceActivity should have thrown an exception when "setContentView" is called. Just because there is a more popular way to use an object doesn't mean that is the only way. – Justin Nov 02 '15 at 16:03
  • Here is a link to where this solution is recommended on StackOverflow. http://stackoverflow.com/a/3026922/1858524 – Justin Nov 02 '15 at 16:16
  • This was obviously not the intended way to use PreferenceActivity. The only difference is that now the side effects are visible. Use this http://developer.android.com/reference/android/support/v7/preference/PreferenceFragmentCompat.html – Eugen Pechanec Nov 02 '15 at 16:17
  • I started seeing something similar when I upgraded from 23.0.1 to 23.1.0 ... it (appears) that setContentView is no longer explicitly inflating nested layouts. All this should mean, is that you need to inflate the layout you're invoking above yourself. – HappyKatz Nov 03 '15 at 10:51
  • Did some more digging on this - the library that seems to introduce the issues is the design support library 23.1.0. Are you using this library ? If so, can you change your gradle configuration from compile 'com.android.support:design:23.1.0' to 'com.android.support:design:23.0.1' ? – HappyKatz Nov 03 '15 at 11:40

0 Answers0