Display colors from an array list in second activity's background = app crash (log provided)

0

I'm new at this and any help is appreciated! -=)

I have colors in an array list that are chosen in the settings activity using checkboxes. I want these to be displayed in the sequence activity's background in a loop.

The colors are from the "colors.xml" values. Currently the app keeps crashing when going from the settings activity to the sequence activity.

Settings Activity:

package com.example.lightbox;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import java.util.ArrayList;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        Button sequence_go = findViewById(R.id.sequence_go);

        CheckBox yellow = findViewById(R.id.yellow_check);
        CheckBox orange = findViewById(R.id.orange_check);
        CheckBox red = findViewById(R.id.red_check);
        CheckBox pink = findViewById(R.id.pink_check);
        CheckBox purple = findViewById(R.id.purple_check);
        CheckBox blue = findViewById(R.id.blue_check);
        CheckBox aqua = findViewById(R.id.aqua_check);
        CheckBox lime = findViewById(R.id.lime_check);
        CheckBox green = findViewById(R.id.green_check);
        CheckBox white = findViewById(R.id.white_check);

        final ArrayList<Integer> colorList = new ArrayList<>();

        white.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                colorList.add(R.color.white);

                Toast.makeText(SettingsActivity.this, "white Checked", Toast.LENGTH_SHORT).show();
            }
        });

        yellow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                colorList.add(R.color.yellow);

                Toast.makeText(SettingsActivity.this, "yellow Checked", Toast.LENGTH_SHORT).show();

            }
        });

        orange.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                colorList.add(R.color.orange);

                Toast.makeText(SettingsActivity.this, "orange Checked", Toast.LENGTH_SHORT).show();

            }
        });

        red.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                colorList.add(R.color.red);

                Toast.makeText(SettingsActivity.this, "red Checked", Toast.LENGTH_SHORT).show();

            }
        });

        pink.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                colorList.add(R.color.pink);

                Toast.makeText(SettingsActivity.this, "pink Checked", Toast.LENGTH_SHORT).show();

            }
        });

        purple.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                colorList.add(R.color.purple);

                Toast.makeText(SettingsActivity.this, "purple Checked", Toast.LENGTH_SHORT).show();

            }
        });

        blue.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                colorList.add(R.color.blue);

                Toast.makeText(SettingsActivity.this, "blue Checked", Toast.LENGTH_SHORT).show();

            }
        });

        aqua.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                colorList.add(R.color.aqua);

                Toast.makeText(SettingsActivity.this, "aqua Checked", Toast.LENGTH_SHORT).show();

            }
        });

        lime.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                colorList.add(R.color.lime);

                Toast.makeText(SettingsActivity.this, "lime Checked", Toast.LENGTH_SHORT).show();

            }
        });

        green.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                colorList.add(R.color.green);

                Toast.makeText(SettingsActivity.this, "green Checked", Toast.LENGTH_SHORT).show();

            }
        });

        sequence_go.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), Sequence_Activity.class);
                intent.putExtra("seqColors", colorList);
                Toast.makeText(SettingsActivity.this, "To Sequence_Activity", Toast.LENGTH_SHORT).show();
                startActivity(intent);
            }
        });
    }
}

Sequence Activity:

package com.example.lightbox;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class Sequence_Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sequence);

        ConstraintLayout sequence = findViewById(R.id.sequence_screen);

        Bundle bundle = getIntent().getExtras();
        int bgColor = bundle.getInt("seqColors", -1);
        ValueAnimator anim = ValueAnimator.ofFloat(0, 1);   // animate from 0 to 1
        anim.setDuration(300);                              // for 300 ms

        sequence.setBackgroundColor(getResources().getColor(bgColor));

        sequence.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
                Toast.makeText(Sequence_Activity.this,"Settings Clicked",Toast.LENGTH_SHORT).show();

                startActivity(intent);
            }

        });
    }
}

Colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="charcoal">#262626</color>
    <color name="yellow">#FFFF00</color>
    <color name="orange">#FF8400</color>
    <color name="red">#FF0000</color>
    <color name="green">#009900</color>
    <color name="lime">#00FF00</color>
    <color name="aqua">#00EEFF</color>
    <color name="blue">#0000ff</color>
    <color name="purple">#8c1aff</color>
    <color name="pink">#ff66ff</color>
    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>
    <color name="grey25">#bfbfbf</color>
    <color name="grey50">#808080</color>
    <color name="grey75">#404040</color>
    <color name="grey80">#333333</color>
    <color name="grey85">#262626</color>
    <color name="grey90">#1a1a1a</color>
    <color name="grey95">#0d0d0d</color>
</resources>

Logcat:

**--------- beginning of crash**

2020-03-06 09:18:30.662 15050-15050/com.example.lightbox E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.lightbox, PID: 15050 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lightbox/com.example.lightbox.Sequence_Activity}: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) Caused by: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:237) at android.content.res.Resources.getColor(Resources.java:982) at android.content.res.Resources.getColor(Resources.java:958) at com.example.lightbox.Sequence_Activity.onCreate(Sequence_Activity.java:25) at android.app.Activity.performCreate(Activity.java:7802) at android.app.Activity.performCreate(Activity.java:7791) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)  at android.os.Handler.dispatchMessage(Handler.java:107)  at android.os.Looper.loop(Looper.java:214)  at android.app.ActivityThread.main(ActivityThread.java:7356)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

java
android-studio
android-intent
colors
bundle
asked on Stack Overflow Mar 5, 2020 by Bob Bobberty • edited Mar 7, 2020 by Bob Bobberty

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0