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

标签 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());
    }

测试偏好代码:

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

最佳答案

只需将应用程序类 TestPreference 添加到您的 list 中。此类必须注册才能正常工作:

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

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

</application>

关于android - 如何从 Android 中的 PreferenceScreen 填充数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35699004/

相关文章:

android - 在android中使用游标从数据库表中获取数据

java - Android 的 PreferenceScreen 替代方案

java - 如何从首选项中的 AlertDialog 获取值

android - 无法保存图像点击时的共享首选项?

android - 获取android中imageview的内容

android - 带有始终可见的选项卡图像的抽屉导航

android - 在 Android 中的 DCIM 文件夹下的 SD 卡上创建文件夹时出错

Android PreferenceScreen 首选项

android - 更改 Android 中首选项类别的文本颜色和水平颜色

android - 使用 Linkify、主题标题启动电子邮件 Intent