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

如何在Android中从PreferenceScreen填充数据?,android,android-preferences,Android,Android Preferences,我想使用此库更改颜色:。我在prefrencesereen中使用此库,我希望使用此组件更改文本颜色!但启动应用程序时显示错误。 LogCat错误: 02-29 15:24:09.541 19631-19631/com.tellfa.mytestpreference E/AndroidRuntime: FATAL EXCEPTION: main

我想使用此库更改颜色:。我在
prefrencesereen
中使用此库,我希望使用此组件更改文本颜色!但启动应用程序时显示错误。
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) 
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());
    }
AppPreference代码:

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) 
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());
    }
设置页面代码:

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) 
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());
    }
主页代码:

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) 
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());
    }
测试首选项代码:

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

如何解决这个问题?我真的需要帮助!谢谢大家只需将应用程序类TestPreference添加到清单中即可。必须注册此类才能正常工作:

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

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

</application>


请显示您的TestPreference类…您在哪里调用getContentTextColor()?上面没有发布,可能是主页类中缺少一些代码片段?@Opiatefuchs,更新上面的帖子。请帮帮我。我真的需要this@Opiatefuchs,您能帮助我吗?我想您必须在TestPreference类中初始化appPreference之前调用super.onCreate()。这只是一个假设。请尝试,从设备中删除应用程序,清理项目,并在进行更改后重新安装。