发生工具栏和状态栏颜色更改错误

Toolbar and Status bar color change error occured

我正在尝试了解 Android 中的 SharedPreference 概念。然后我写了一些代码。但是当我 运行 时,我遇到了以下错误。

01-03 17:56:33.915 4607-4607/com.example.anu.sharedpreference E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.anu.sharedpreference, PID: 4607 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setBackgroundColor(int)' on a null object reference at com.example.anu.sharedpreference.MainActivity.onClick(MainActivity.java:31) at android.view.View.performClick(View.java:6256) at android.view.View$PerformClick.run(View.java:24701) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

我选择的颜色保存在 color.xml 文件中

我的应用看起来像那样

image of layout

    package com.example.anu.sharedpreference;
    import android.content.SharedPreferences;
    import android.os.Build;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.widget.Button;

    public class MainActivity extends AppCompatActivity {
        Toolbar mToolbar;
        Button btnRed, btnGreen, btnYellow;

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

            btnRed = (Button)findViewById(R.id.btnRed);
            btnGreen = (Button)findViewById(R.id.btnGreen);
            btnYellow = (Button)findViewById(R.id.btnYellow);

            if (getColor() != getResources().getColor(R.color.colorPrimary)) {
                mToolbar.setBackgroundColor(getColor());
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
                    getWindow().setStatusBarColor(getColor());
                }
            }

            btnRed.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mToolbar.setBackgroundColor(getResources().getColor(R.color.colorRed));
                    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
                        getWindow().setStatusBarColor(getResources().getColor(R.color.colorRed));
                    }
                    storeColor(getResources().getColor(R.color.colorRed));
                }
            });

            btnGreen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mToolbar.setBackgroundColor(getResources().getColor(R.color.colorGreen));
                    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
                        getWindow().setStatusBarColor(getResources().getColor(R.color.colorGreen));
                    }
                    storeColor(getResources().getColor(R.color.colorGreen));
                }
            });

            btnYellow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mToolbar.setBackgroundColor(getResources().getColor(R.color.colorYellow));
                    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
                        getWindow().setStatusBarColor(getResources().getColor(R.color.colorYellow));
                    }
                    storeColor(getResources().getColor(R.color.colorGreen));
                }
            });
        }
        private void storeColor(int color){
            SharedPreferences mSharedPreferences = getSharedPreferences("ToolbarColor", MODE_PRIVATE);
            SharedPreferences.Editor mEditor = mSharedPreferences.edit();
            mEditor.putInt("Color",color);
            mEditor.apply();
        }
        private int getColor (){
            SharedPreferences mSharedPreferences = getSharedPreferences("ToolbarColor", MODE_PRIVATE);
            int selectedColor = mSharedPreferences.getInt("Color", getResources().getColor(R.color.colorPrimary));
            return selectedColor;
        }
    }

我能知道如何解决这个问题吗?

您只是忘记在 onCreate() 方法

中对您的 Toolbar 执行 findViewById
 toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
 setSupportActionBar(toolbar);