更改线性布局背景颜色时崩溃

时间:2018-01-24 20:50:05

标签: 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) {

    }
}

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

1 个答案:

答案 0 :(得分:1)

您正在尝试将LinearLayout投射到ConstraintLayout

在您的活动中将行LinearLayout paramLayout;更改为ConstraintLayout paramLayout;