3

I am following these instructions to create a PreferenceActivity but the PereferenceActivity is not displayed. Activity starts but the Layout is blank.

  1. I am also using material design NavDrawer in my Application by referring to developer.android.com but i hope it doesn't affect
  2. I am testing application on device running Kitkat
  3. Here is what happens when i launch PreferenceActivity "screenshot"
  4. Logcat: Could not find method android.preference.PreferenceActivity.onCreate VFY: unable to resolve virtual method 950: Landroid/preference/PreferenceActivity;.onCreate(Landroid/os/Bundle;Landroid/os/PersistableBundle;)V

Code:

app_preference.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Settings">
    <ListPreference
            android:title="Timer"
            android:summary="@string/timer_summary"
            android:key="prefTime"
            android:defaultValue="30"
            android:entries="@array/prefTime"
            android:entryValues="@array/prefTimeValue"/>
    <SwitchPreference
            android:title="Shuffle"
            android:summary="@string/shuffle_summary"
            android:defaultValue="false"
            android:key="prefToggleShuffle"/>
    <SwitchPreference
            android:title="Allow over Wifi-only"
            android:summary="@string/wifi_only_summary"
            android:defaultValue="false"
            android:key="prefWifiOnly"/>
    <Preference
            android:title="Restore Defaults"
            android:summary="@string/restore_summ"/>
    <Preference
            android:title="Clear Data"
            android:summary="@string/shuffle_summary"/>
</PreferenceCategory>

Settings.java

public class Settings extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}

public static class SettingsFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.app_preference);
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            break;
    }
    return super.onOptionsItemSelected(item);
}

}

Mainfest.xml

.....
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21"/>
<activity android:name=".preferences.Settings>
        <intent-filter>
            <category android:name="android.intent.category.PREFERENCE"/>
        </intent-filter>
        <meta-data android:name="android.support.PARENT_ACTIVITY"
                   android:value=".activity.MainActivity"/>
    </activity>
.....

Calling Intent

case R.id.action_setting:
            Intent intent = new Intent(this, Settings.class);
            startActivityForResult(intent, RESULT_SETTINGS);//RESULT_SETTINGS = 1
            break;
harsh_v
  • 3,193
  • 3
  • 34
  • 53

4 Answers4

9

Delete the second parameter in onCreate method:

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

Update for Aviel Fedida: According with this, void onCreate (Bundle savedInstanceState, PersistableBundle persistentState) is called when the activity is being re-initialized. So when you start a new Activity with startActivityForResult or startActivity that method is not called, and the code inside is not executed.

Black screen appears becouse addPreferencesFromResource(R.xml.app_preference); is "unreachable code"

cgr
  • 519
  • 1
  • 7
  • 20
  • Hi, I had this problem as well, your solution works for me but, can you explain in your answer why the black screen appears when you use the constructor version with `PersistableBundle`, or maybe reference the some article that explains, thanks. – Aviel Fedida Jul 22 '16 at 10:26
  • Hahah, liked the "Update for me", thanks so much, have a nice day! – Aviel Fedida Jul 25 '16 at 10:25
2

Use protected void onCreate(Bundle savedInstanceState)instead of public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

Son Nguyen Thanh
  • 1,199
  • 15
  • 19
1

I had the same problem and I solved it by adding the line:

setContentView(R.layout.activity_settings);

Here I mean to add the layout file associated to your SettingsActivity, inside the OnCreate method of the Settings class:

enter image description here

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
0

[RE-EDITED]

The main issue seems to be that you are using a PreferenceActivity with a PreferenceFragment, and you are assuming that a PreferenceActivity has things like action bars that you can directly access. This is probably a bad assumption.

A PreferenceFragment is actually meant to be used with a "regular" Activity, not a PreferenceActivity.

So, for instance, if you really want to access the action bar, you should use an ActionBarActivity instead. See also the documentation in the API Guide.

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • already tried that ,doesn't work. Results in multiple null pointer exception across MainActivity super.onCreate(savedInstanceState); and getSupportActionBar() method – harsh_v Jan 30 '15 at 00:44
  • I followed documentation, removed actionBar, used a regular activity but still doesn't work in fact every time application crashes with Null Pointer exception as i told earlier. – harsh_v Jan 30 '15 at 01:39
  • OK, I have modified my answer again. If you still have issues, please submit another question with full details. – cybersam Jan 30 '15 at 01:48