пытаюсь программно изменить цвет кнопки:
btnAnswer1.setBackgroundColor(getResources().getColor(R.color.whiteDark));
цвет такой описан в ресурсах:
<color name="whiteDark">#2D2D2D</color>
однако выдает ошибку:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setBackgroundColor(int)' on a null object reference
at example.englishapp.MainActivity.onCreate(MainActivity.java:92)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Пытался также изменить цвет кнопки следующим образом:
btnAnswer1.setBackgroundColor(btnAnswer1.getContext().getResources().getColor(R.color.whiteDark));
Выдает такую-же ошибку. Подскажите, что я делаю не так? Обычное изменение цвета кнопки при помощи btnAnswer1.setBackgroundColor(Color.RED)
и т.п. не устраивает, т.к. нужного мне цвета нет в библиотеке
Также пытался установить фон кнопки при помощи btnAnswer1.setBackground(Drawable.createFromPath("@drawable/btn_top_border"));
в ресурсах предварительно описан фон:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FFFFFF"/>
</shape> </item> <item android:top="1dp">
<shape android:shape="rectangle"> <solid android:color="#2D2D2D"/>
</shape>
</item>
</layer-list>
Код MainActivity:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences sp = getSharedPreferences(MY_SETTINGS, Context.MODE_PRIVATE);
boolean hasVisited = sp.getBoolean("hasVisited", false);
themen = sp.getString("themen", "Night");
btnAnswer1.setBackground(Drawable.createFromPath("@drawable/btn_top_border"));
if (!hasVisited) {
writeFile();
readFile();
startActivity(new Intent(this, FirstStartActivity.class));
SharedPreferences.Editor e = sp.edit();
e.putBoolean("hasVisited", true);
e.commit();}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ct1 = (ConstraintLayout) findViewById(R.id.cl1);
btnSound = (ImageButton) findViewById(R.id.btnSound);
btnMenu = (ImageButton) findViewById(R.id.btnMenu);
tvLearningWords = (TextView) findViewById(R.id.tvLearningWords);
tvTranscription = (TextView) findViewById(R.id.tvTranscription);
setTitle("Новые слова");
btnAnswer1 = (Button) findViewById(R.id.btnAnswer1);
btnAnswer2 = (Button) findViewById(R.id.btnAnswer2);
btnAnswer3 = (Button) findViewById(R.id.btnAnswer3);
btnAnswer4 = (Button) findViewById(R.id.btnAnswer4);
btnAnswer5 = (Button) findViewById(R.id.btnAnswer5);
btnSound.setOnClickListener(this);
btnMenu.setOnClickListener(this);
btnAnswer1.setOnClickListener(this);
btnAnswer2.setOnClickListener(this);
btnAnswer3.setOnClickListener(this);
btnAnswer4.setOnClickListener(this);
btnAnswer5.setOnClickListener(this);}
btnAnswer1
-null
. Наверное вы перепутали ID в разметке.MainActivity.onCreate
и разметку покажите.