javaandroidandroid-seekbar

SeekBar RGB picker keeps stoping


I am trying to do app, where you have 3 SeekBars and under them, there are 3 TextFields, which are showing shade of each color, and under those is TextField, which is changing color when you are moving SeekBar. When I try to lunch it on mobile, it say's that the app "Keeps stopping". Thanks for help.

<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:orientation="vertical">


    <SeekBar
        android:id="@+id/SB1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="183dp"
        android:max="255"></SeekBar>

    <SeekBar
        android:id="@+id/SB2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="114dp"
        android:max="255"></SeekBar>

    <SeekBar
        android:id="@+id/SB3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp"
        android:max="255"></SeekBar>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginTop="279dp">

        <TextView
            android:id="@+id/TVr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="false"
            android:layout_alignParentTop="false"
            android:layout_gravity="left"
            android:text="R = 0"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/TVg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="false"
            android:layout_alignParentTop="false"
            android:layout_gravity="center"
            android:text="G = 0"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/TVb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="false"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="false"
            android:layout_gravity="right"
            android:text="B = 0"
            android:textSize="30sp" />

    </LinearLayout>

    <TextView
        android:id="@+id/vysledek"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="67dp" />

</RelativeLayout>
package com.example.posuvnik;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    SeekBar SB1;
    SeekBar SB2;
    SeekBar SB3;
    TextView vysledek;
    TextView TVr, TVb, TVg;

    private int seekR, seekG, seekB;

    public class MySeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            switch (seekBar.getId()) {
                case R.id.SB1:
                    seekR = progress;
                    TVr.setText(seekR);
                    break;
                case R.id.SB2:
                    seekG = progress;
                    TVg.setText(seekG);
                    break;
                case R.id.SB3:
                    seekB = progress;
                    TVb.setText(seekB);
                    break;
            }
            int barva = Color.rgb(seekR,seekG,seekB);
            vysledek.setBackgroundColor(barva);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_changer);

        SeekBar sbR = (SeekBar) findViewById(R.id.SB1);
        SeekBar sbG = (SeekBar) findViewById(R.id.SB2);
        SeekBar sbB = (SeekBar) findViewById(R.id.SB3);

        MySeekBarChangeListener msbclR = new MySeekBarChangeListener();
        MySeekBarChangeListener msbclG = new MySeekBarChangeListener();
        MySeekBarChangeListener msbclB = new MySeekBarChangeListener();

        sbR.setOnSeekBarChangeListener(msbclR);
        sbG.setOnSeekBarChangeListener(msbclG);
        sbB.setOnSeekBarChangeListener(msbclB);

    }

}

Solution

  • You didn't initialize text views...

    TVr = findViewById(R.id.TVr); - do it for all 3 TextViews TVr, TVg, TVb;

    Also: