Как сделать текстовые представления видимыми в зависимости от прогресса панели поиска - PullRequest
0 голосов
/ 08 мая 2020

Я пытаюсь сделать 4 разных текстовых окна видимыми в зависимости от хода моей панели поиска (по одному за раз). Я установил текстовые представления невидимыми, а максимальную полосу поиска - 100. Он отлично работает для значений ниже 24, но приложение вылетает, как только полоса поиска превышает 25.

Я абсолютный начинающий программист и пытаюсь научить я думал использовать вместо этого while l oop, но я тоже не могу заставить его работать.

Буду очень признателен за любую помощь.

Код:

public class MainActivity extends AppCompatActivity {

SeekBar seekBar;
TextView textView;
TextView textView2;
TextView textView3;
TextView textView4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    seekBar = findViewById(R.id.seekBar);
    seekBar.setMax(100);
    textView = (TextView)findViewById(R.id.textView);
    button = (Button)findViewById(R.id.button);

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                if (seekBar.getProgress() < 24) {
                    textView.setVisibility(View.VISIBLE);
                } else if (seekBar.getProgress() >= 25 && seekBar.getProgress() < 49) {
                    textView2.setVisibility(View.VISIBLE);
                } else if (seekBar.getProgress() >= 50 && seekBar.getProgress() < 74) {
                    textView3.setVisibility(View.VISIBLE);
                } else if (seekBar.getProgress() >= 75) {
                    textView4.setVisibility(View.VISIBLE);
                }

activity_main:

<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

1 Ответ

1 голос
/ 08 мая 2020

Вы использовали findViewById, чтобы связать ресурс с вашим textView объектом. Вам нужно сделать то же самое для textView2, textView3 и textView4. Я предполагаю, что вы получаете NullPointerException, когда пытаетесь вызвать метод на textView2.

...