Search code examples
javaandroidpreferencefragment

preference screen does not show up. Empty screen shows up on launching activity


I have created two activities. One to hold preference fragment and one to hold button. On click of button it should display preference fragment. There is no error showing up but when I click on the button an empty screen pops up instead of the settings layout I've created. I've tried everything on this website but nothing works. I've included the code here and would be really grateful for some help. MainActivity.java

    package com.example.vavi.settings;

import android.content.Intent;
import android.preference.PreferenceFragment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn=(Button)findViewById(R.id.btn);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new 
Intent(getApplicationContext(),defaultPref.class));
        }
    });
}

}

This is the code for the second activity defaultPref.java

    package com.example.vavi.settings;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.support.annotation.Nullable;

/**
 * Created by VAVI on 4/4/2018.
 */

public class defaultPref extends PreferenceActivity {
    @Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable 
PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.default_pref);

    getFragmentManager().beginTransaction().replace(R.id.rep, new 
PrefFrag()).commit();
}

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

I have included the second activity in Manifest file. But when I click on the button an empty screen shows up.


Solution

  • Replace your code with this:

     @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.default_pref);
    
        getFragmentManager().beginTransaction().replace(R.id.rep, new 
        PrefFrag()).commit();
        }
    

    Remove this method:

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable 
    PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.default_pref);
    
        getFragmentManager().beginTransaction().replace(R.id.rep, new 
    PrefFrag()).commit();
    }
    

    Your final code:

    public class defaultPref extends PreferenceActivity {
        @Override
            protected void onCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.default_pref);
    
            getFragmentManager().beginTransaction().replace(R.id.rep, new 
            PrefFrag()).commit();
            }
    
    public static class PrefFrag extends PreferenceFragment{
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preference);
        }
      }
    }