如何从Android中的PreferenceScreen填充数据?

时间:2016-02-29 12:00:09

标签: android android-preferences

我想使用此库进行更改颜色:Color Picker。我在PrefrenceSreen中使用此库,我希望使用此组件更改文本颜色!但是在开始申请时会显示错误 LogCat错误:

02-29 15:24:09.541 19631-19631/com.tellfa.mytestpreference E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.tellfa.mytestpreference, PID: 19631
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tellfa.mytestpreference/com.tellfa.mytestpreference.MainPage}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.tellfa.mytestpreference.AppPreference.getContentTextColor()' on a null object reference
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
                                                                                 at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5349)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
                                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.tellfa.mytestpreference.AppPreference.getContentTextColor()' on a null object reference
                                                                                 at com.tellfa.mytestpreference.MainPage.ContentTextColor(MainPage.java:66)
                                                                                 at com.tellfa.mytestpreference.MainPage.onCreate(MainPage.java:46)
                                                                                 at android.app.Activity.performCreate(Activity.java:6020)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2284)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 
                                                                                 at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:135) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5349) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 

AppPreference代码:

public class AppPreference {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private Context context;

    private static final String KeyContentTextColor = "textColor_pref";

    public AppPreference(Context context){
        this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        this.editor = sharedPreferences.edit();
        this.context = context;
    }

    public int getContentTextColor(){
        return sharedPreferences.getInt(KeyContentTextColor, context.getResources().getColor(R.color.textDark));
    }

    public void setContentTextColor(Integer res){
        editor.putInt(KeyContentTextColor, res);
        editor.commit();
    }
}

设置网页代码:

public class SettingPage extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

    private AppCompatDelegate mDelegate;
    private AppPreference myAppPreference;
    private Context context;
    private Toolbar toolbar;
    private AmbilWarnaPreference ContentTextColor_pref;
    private Preference defaultColor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getmDelegate().installViewFactory();
        getmDelegate().onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.toolbar_setting);
        toolbar = (Toolbar) findViewById(R.id.setting_toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        addPreferencesFromResource(R.xml.pref_setting);
        //getFragmentManager().beginTransaction().replace(android.R.id.content, new myPreferenceFragment()).commit();

        this.context = this;
        this.myAppPreference = TestPreference.getAppPreference();

        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
        pref.registerOnSharedPreferenceChangeListener(this);

        ContentTextColor_pref = (AmbilWarnaPreference) findPreference("textColor_pref");
        defaultColor = findPreference("prefDefaultValues");
        defaultColor.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                myAppPreference.setContentTextColor(getResources().getColor(R.color.textDark));
                return true;
            }
        });
    }

主页代码:

public class MainPage extends AppCompatActivity {
    private Toolbar toolbar;
    private WebView webView;
    private TextView textView;
    private AppPreference appPreference;
    private Context context;
    private Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page);

        this.appPreference = TestPreference.getAppPreference();
        this.activity = this;
        this.context = this;

        toolbar = (Toolbar) findViewById(R.id.main_toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle("Main Page");
        }
        //toolbar.setLogo(R.drawable.ic_menu_toolbar);
        toolbar.setLogoDescription("Logo");
        toolbar.setTitle("Main Page");
        toolbar.setSubtitle("Home Page");
        toolbar.inflateMenu(R.menu.main_manu);

        textView = (TextView) findViewById(R.id.main_textView);
        textView.setTextColor(appPreference.getContentTextColor());
    }

TestPreference代码:

public class TestPreference extends Application {
    private static AppPreference appPreference;

    @Override
    public void onCreate() {
        appPreference = new AppPreference(this);
        super.onCreate();
    }

    public static AppPreference getAppPreference(){
        return appPreference;
    }
}

如何解决这个问题?我真的需要帮助!谢谢所有< 3

1 个答案:

答案 0 :(得分:1)

只需将Application类TestPreference添加到清单中即可。必须注册此类才能正常工作:

     <application 
    android:icon="@drawable/yourAppIcon"
    android:label="@string/yourAppName" 
    android:name="yourPackage.TestPreference">

              <!-- your other classes/services etc between application tag -->

</application>