gpt4 book ai didi

android - 更改线性版式背景颜色时崩溃

转载 作者:行者123 更新时间:2023-12-03 16:44:56 25 4
gpt4 key购买 nike

我正在尝试更改布局的背景颜色,以使单击按钮时出现深色/浅色主题。但是,当我单击按钮时,我的应用程序崩溃了,我也不知道为什么。

布局XML代码:

<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代码:
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) {

}
}

我尝试了不同的方法,但是每次我的应用程序崩溃时。有人会对这个问题有想法吗?我必须在学校功课上这样做,但我不知道如何解决这个问题。

最佳答案

您正在尝试将LinearLayout转换为ConstraintLayout
在您的 Activity 中将LinearLayout paramLayout;行更改为ConstraintLayout paramLayout;

关于android - 更改线性版式背景颜色时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48431362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com