Im работает над небольшим проектом с 2 мероприятиями. Я хочу, чтобы нажать кнопку на одном из действий, и это изменит цвет фона всех действий на # 264A73. Пока что у меня есть;Как изменить цвет фона деятельности с помощью кнопки из другой деятельности
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class settings extends AppCompatActivity {
private Button about, goDark;
private RelativeLayout settingsLayout, aboutUs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_settings);
about=(Button) findViewById(R.id.aboutButton);
goDark=(Button) findViewById((R.id.dark));
settingsLayout = (RelativeLayout) findViewById(R.id.settingsActivity);
aboutUs = (RelativeLayout) findViewById((R.id.aboutActivity));
about.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(settings.this, AboutUs.class);
startActivity(intent);
}
});
goDark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
settingsLayout.setBackgroundColor(Color.BLUE);
aboutUs.setBackgroundColor(Color.BLUE);
}
});
}
Он изменяет цвет текущей деятельности (настройки) к синему, так как я не был уверен, как использовать значение RGB, но дает мне исключения нулевого указателя при попытке изменить цвет фона активности AboutUs "java.lang.NullPointerException: попытка вызвать виртуальный метод void android.widget.RelativeLayout.setBackgroundColor (int)" в ссылке на нулевой объект ". Любая помощь, добавляющая переменную RGB и прекращение исключения нулевого указателя, будет с благодарностью оценена.
редактировать:
идентификаторы я использовал, SettingsActivity и AboutActivity определяются как идентификаторы относительных компоновок файлов XML ниже:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/settingsActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About Us"
android:id="@+id/aboutButton"
android:layout_alignParentTop="true"
android:layout_marginTop="52dp"
android:layout_alignParentEnd="true"
android:background="#CACFD9"
android:bottomRightRadius="50dp"
android:bottomLeftRadius="50dp"
android:topLeftRadius="50dp"
android:topRightRadius="50dp"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You use darkness powder"
android:id="@+id/dark"
android:layout_marginTop="66dp"
android:background="#CACFD9"
android:bottomRightRadius="50dp"
android:bottomLeftRadius="50dp"
android:topLeftRadius="50dp"
android:topRightRadius="50dp"
android:layout_below="@+id/aboutButton"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/aboutActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<ImageButton
android:layout_width="130dp"
android:layout_height="110dp"
android:id="@+id/rachel_button"
android:background="@android:color/transparent"
android:src="@drawable/piecer"
android:layout_marginStart="27dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="130dp"
android:layout_height="110dp"
android:id="@+id/thomas_button"
android:src="@drawable/piecet"
android:background="@android:color/transparent"
android:layout_marginTop="39dp"
android:layout_below="@+id/rachel_button"
android:layout_alignStart="@+id/rachel_button" />
<ImageButton
android:layout_width="130dp"
android:layout_height="110dp"
android:id="@+id/hannah_button"
android:src="@drawable/pieceh"
android:background="@android:color/transparent"
android:layout_marginTop="41dp"
android:layout_below="@+id/thomas_button"
android:layout_alignStart="@+id/thomas_button" />
<ImageButton
android:layout_width="130dp"
android:layout_height="110dp"
android:id="@+id/blaine_button"
android:src="@drawable/pieceb"
android:background="@android:color/transparent"
android:layout_marginTop="39dp"
android:layout_below="@+id/hannah_button"
android:layout_alignStart="@+id/hannah_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Rachel Humphrey - Team Leader, part-time sorcerer"
android:id="@+id/textView4"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/rachel_button"
android:layout_marginTop="31dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Thomas McIlvenna - Programmer, full-time warrior troll"
android:id="@+id/textView5"
android:layout_marginTop="35dp"
android:layout_alignTop="@+id/thomas_button"
android:layout_toEndOf="@+id/thomas_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hannah McClure - Programmer, dwarf extraordinaire"
android:id="@+id/textView6"
android:layout_marginTop="35dp"
android:layout_alignTop="@+id/hannah_button"
android:layout_toEndOf="@+id/hannah_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Blaine Crooks - Programmer, bag carrier"
android:id="@+id/textView7"
android:layout_marginTop="35dp"
android:layout_alignTop="@+id/blaine_button"
android:layout_toEndOf="@+id/blaine_button" />
</RelativeLayout>
так в моем файле настроек бы я его продлить BaseActivity и просто вызовите settingsLayout.changeAppBackground (# 264A73); внутри моего OnClickListener? – crooks94
В OnClickListener вам нужно будет вызвать 'changeAppBackground (0xff264a73); updateBackgroundColor(); ' –
Ты абсолютная легенда, мой друг, много часов работал над этим хахом, большое спасибо – crooks94