Reputation: 23
I'm trying to change the background color of my layout to make dark/light theme on button click. But when I click on my button, my app crash and I don't know why.
Layout XML code :
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layoutParam"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bleuNuit"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="mcamus.ihm_smartphone.Parametres"
tools:showIn="@layout/app_bar_parametres">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginTop="24dp"
android:text="Thème de l'application"
android:textColor="@color/texteBlanc"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/thmClair"
android:onClick="thmClair"
android:layout_width="64dp"
android:layout_height="32dp"
android:layout_marginStart="48dp"
android:layout_marginTop="18dp"
android:text="Clair"
android:textSize="20px"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/thmSombre"
android:onClick="thmSombre"
android:layout_width="64dp"
android:layout_height="32dp"
android:layout_marginStart="120dp"
android:layout_marginTop="18dp"
android:text="Sombre"
android:textSize="20px"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent" />
Java code :
public class Parametres extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
LinearLayout paramLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parametres);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.parametres, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.alimentation) {
Intent alimentation = new Intent(Parametres.this,Accueil.class);
startActivity(alimentation);
} else if (id == R.id.capteurs) {
Intent capteurLayout = new Intent(Parametres.this,Capteurs.class);
startActivity(capteurLayout);
} else if (id == R.id.absence) {
Intent absLayout = new Intent(Parametres.this,Absence.class);
startActivity(absLayout);
} else if (id == R.id.parametres) {
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void thmSombre(View view) {
paramLayout = findViewById(R.id.layoutParam);
paramLayout.setBackgroundColor((Color.parseColor("#FFFFFF")));
}
public void thmClair(View view) {
}
}
I tried different method, but each time my application crashes. Someone would have an idea of the problem? I have to do this on homework for school and I do not know how to solve this problem.
Upvotes: 0
Views: 853
Reputation: 1373
You are trying to cast LinearLayout
into a ConstraintLayout
Change line LinearLayout paramLayout;
to ConstraintLayout paramLayout;
in your activity
Upvotes: 1