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

标签 android android-studio crash android-linearlayout background-color

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

布局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/

相关文章:

android - 如何增加 Android PJSIP 中的麦克风音量?

android - 尝试将数据加载到共享首选项或从共享首选项加载数据时,应用程序崩溃

javascript - 为什么 deviceready 的事件监听器没有在我的手机上触发?

android - 为什么总是 TECH_DISCOVERED 或 TAG_DISCOVERED 而不是 NDEF_DISCOVERED?

android - 如何使用带有 aar 库的人行横道嵌入 XWalkView

android - Google Cloud Messaging GCM 可靠吗?

java - 在 android studio 中使用 2D 数组

java - 连接设备后Android studio界面不重绘

Android Parse 没有收到推送通知

ios - 在物理设备上测试我的应用程序(使用 swift)时崩溃,线程 1 : EXC_BREAKPOINT (code=1, 子代码 = 0x120021088)