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

时间:2018-01-03 12:32:26

标签: android nullpointerexception sharedpreferences android-toolbar android-statusbar

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

  

01-03 17:56:33.915 4607-4607 / com.example.anu.sharedpreference E / AndroidRuntime:FATAL EXCEPTION:main                                                                                   处理:com.example.anu.sharedpreference,PID:4607                                                                                   java.lang.NullPointerException:尝试调用虚方法' void android.support.v7.widget.Toolbar.setBackgroundColor(int)'在null对象引用上                                                                                       在com.example.anu.sharedpreference.MainActivity $ 1.onClick(MainActivity.java:31)                                                                                       在android.view.View.performClick(View.java:6256)                                                                                       在android.view.View $ PerformClick.run(View.java:24701)                                                                                       在android.os.Handler.handleCallback(Handler.java:789)                                                                                       在android.os.Handler.dispatchMessage(Handler.java:98)                                                                                       在android.os.Looper.loop(Looper.java:164)                                                                                       在android.app.ActivityThread.main(ActivityThread.java:6541)                                                                                       at java.lang.reflect.Method.invoke(Native Method)                                                                                       在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                                                       在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;
        }
    }

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

1 个答案:

答案 0 :(得分:1)

您忘记在findViewById方法ToolbaronCreate() {/ 1}}

 toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
 setSupportActionBar(toolbar);