当我运行应用程序时,为什么会出错?

时间:2019-05-20 20:47:01

标签: java android

我想创建一个应用程序,其想法是单击按钮并更改图像。每次我单击按钮时,imageview都会更改(动态imageview)。我正在尝试这样做,但是当我运行第一个imageview下方的代码时,当我按下按钮时,第一个imageview跳转到最后一个imageview,而忽略其中的两个imageview。怎么了?

这是我的代码:

 SEGUNDATELA. JAVA:

       public class SegundaTela extends AppCompatActivity {
        private Integer [] imagens = new Integer[]{R.drawable.tabeladia2, R.drawable.tabeladia3, R.drawable.tabeladia4, R.drawable.tabeladia5};
        private RadioGroup radioGroup;
        private RadioButton sim;
        private RadioButton nao;
        private Button proxima;
        private ImageView img;
        private int i=0;
        private Integer [] dados= new Integer[4];
        private int soma =0;
        private int j;
        private int inicio;


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

            img = findViewById(R.id.imageView);
            proxima = findViewById(R.id.proximaId);
            radioGroup = findViewById(R.id.RadioGroupId);
            sim = findViewById(R.id.simId);
            nao = findViewById(R.id.naoId);

            if (sim.isChecked()) {
                inicio = 1;
            } else if (nao.isChecked()) {
                inicio = 0;
            }


            proxima.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (i < 4) {
                        ++i;
                        j = (i - 1);

                        switch (j) {

                            case 0:

                                if (sim.isChecked()) {
                                    img.setImageResource(imagens[j]);
                                    dados[j] = 2;

                                } else if (nao.isChecked()) {
                                    img.setImageResource(imagens[j]);
                                    dados[j] = 0;
                                }
                                break;
                            case 1:

                                if (sim.isChecked()) {
                                    img.setImageResource(imagens[j]);
                                    dados[j] = 4;

                                } else if (nao.isChecked()) {
                                    img.setImageResource(imagens[j]);
                                    dados[j] = 0;
                                }
                                break;
                            case 2:

                                if (sim.isChecked()) {
                                    img.setImageResource(imagens[j]);
                                    dados[j] = 8;

                                } else if (nao.isChecked()) {
                                    img.setImageResource(imagens[j]);
                                    dados[j] = 0;
                                }
                                break;
                            case 3:

                                if (sim.isChecked()) {
                                    img.setImageResource(imagens[j]);
                                    dados[j] = 16;

                                } else if (nao.isChecked()) {
                                    img.setImageResource(imagens[j]);
                                    dados[j] = 0;
                                }
                                break;

                        }
                        radioGroup.clearCheck();

                    } else {
                        soma = dados[0] + dados[1] + dados[2] + dados[3] + inicio;
                        Intent i = new Intent(SegundaTela.this, MainActivity.class);
                        i.putExtra("soma", soma);
                        startActivity(i);
                    }
                }


            });

        }
        }

SEGUNDATELA.MML

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/proximaId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:text="proxima"
        app:layout_constraintBottom_toTopOf="@+id/imageView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/tabeladia1" />

    <RadioGroup
        android:id="@+id/RadioGroupId"
        android:layout_width="98dp"
        android:layout_height="86dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/imageView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RadioButton
            android:id="@+id/simId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Sim" />

        <RadioButton
            android:id="@+id/naoId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Não" />
    </RadioGroup>

2 个答案:

答案 0 :(得分:1)

每次执行onClick()时,都会执行整个for循环。这就是为什么。如果您只希望它一次只能处理一张图像,则需要找到一种不同的解决方案,使您可以在两次“单击”之间保持状态(知道当前图像)。

答案 1 :(得分:0)

您需要删除for循环并在每次单击按钮时递增。

如果要循环显示图像,请使用此选项。

public class MainActivity extends AppCompatActivity {
    private int [] imagens = {R.drawable.tabeladia2, R.drawable.tabeladia3,
            R.drawable.tabeladia4, R.drawable.tabeladia5};

    private Button proxima;
    private ImageView img;
    private Integer currentImg;

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

        proxima = findViewById(R.id.proximaId);

        img = findViewById(R.id.imageView);

        proxima.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentImg != null && currentImg < 3) {
                    currentImg++;
                } else {
                    currentImg = 0;
                }
                img.setImageResource(imagens[currentImg]);
            }
        });
    }
}

如果您不希望图像循环播放,请使用此选项

    public class MainActivity extends AppCompatActivity {
            private int [] imagens = {R.drawable.tabeladia2, R.drawable.tabeladia3,
                    R.drawable.tabeladia4, R.drawable.tabeladia5};

            private Button proxima;
            private ImageView img;
            private Integer currentImg;

            private int[] intArray = new int[4];

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

                proxima = findViewById(R.id.proximaId);

                img = findViewById(R.id.imageView);

                proxima.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (currentImg != null) {
                            if (currentImg < 3) {
                                currentImg++;
                                if(currentImg == 1){
                                  intArray[1] = 2;
                                }else if(currentImg == 2){
                                  intArray[2] = 4;
                                }else if(currentImg == 3){
                                  intArray[3] = 8;}
                                img.setImageResource(imagens[currentImg]);
                            }else{
                                //handle last image reached condition
                                Toast.makeText(MainActivity.this, "Last image reached", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            currentImg = 0;
                            intArray[0] = 1;
                            img.setImageResource(imagens[currentImg]);
                        }
                    }
                });
            }
        }

使用案例声明和单选按钮。

public class SegundaTela extends AppCompatActivity {
    private Integer[] imagens = new Integer[]{R.drawable.tabeladia2, R.drawable.tabeladia3, R.drawable.tabeladia4, R.drawable.tabeladia5};
    private RadioGroup radioGroup;
    private RadioButton sim;
    private RadioButton nao;
    private Button proxima;
    private ImageView img;
    private Integer i;
    private int soma = 0;


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

        img = findViewById(R.id.imageView);
        proxima = findViewById(R.id.proximaId);
        radioGroup = findViewById(R.id.RadioGroupId);
        sim = findViewById(R.id.simId);
        nao = findViewById(R.id.naoId);


        proxima.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!sim.isChecked() && !nao.isChecked()) {
                    Toast.makeText(SegundaTela.this, "Select an option", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (i != null) {
                    if (i < 5) {
                        switch (i) {
                            case 0:

                                if (sim.isChecked()) {
                                    img.setImageResource(imagens[i]);
                                    //dados[j] = 2;
                                    soma = soma + 2;

                                } else if (nao.isChecked()) {
                                    img.setImageResource(imagens[i]);
                                    //dados[j] = 0;
                                    soma = soma + 0;
                                }
                                break;
                            case 1:

                                if (sim.isChecked()) {
                                    img.setImageResource(imagens[i]);
                                    //dados[j] = 4;
                                    soma = soma + 4;

                                } else if (nao.isChecked()) {
                                    img.setImageResource(imagens[i]);
                                    //dados[j] = 0;
                                    soma = soma + 0;
                                }
                                break;
                            case 2:

                                if (sim.isChecked()) {
                                    img.setImageResource(imagens[i]);
                                    //dados[j] = 8;
                                    soma = soma + 8;

                                } else if (nao.isChecked()) {
                                    img.setImageResource(imagens[i]);
                                    //dados[j] = 0;
                                    soma = soma + 0;
                                }
                                break;
                            case 3:

                                if (sim.isChecked()) {
                                    img.setImageResource(imagens[i]);
                                    //dados[j] = 16;
                                    soma = soma + 16;

                                } else if (nao.isChecked()) {
                                    img.setImageResource(imagens[i]);
                                    //dados[j] = 0;
                                    soma = soma + 0;
                                }
                                break;
                            case 4:
                                /*if (sim.isChecked()) {
                                    //dados[j] = 16;
                                    soma = soma + 32;

                                } else if (nao.isChecked()) {
                                    //dados[j] = 0;
                                    soma = soma + 0;
                                }*/
                                Intent i = new Intent(SegundaTela.this, MainActivity.class);
                                i.putExtra("soma", soma);
                                startActivity(i);
                                break;
                        }
                        ++i;
                        Toast.makeText(SegundaTela.this, "soma: " + soma, Toast.LENGTH_SHORT).show();
                        radioGroup.clearCheck();

                    } /*else {
                        //soma = dados[0] + dados[1] + dados[2] + dados[3] + inicio;
                        Intent i = new Intent(SegundaTela.this, MainActivity.class);
                        i.putExtra("soma", soma);
                        startActivity(i);
                    }*/
                }else {
                    if (sim.isChecked()) {
                        //inicio = 1;
                        soma = soma + 1;
                    } else if (nao.isChecked()) {
                        //inicio = 0;
                        soma = soma + 0;
                    }
                    i = 0;
                    radioGroup.clearCheck();
                }
            }


        });

    }
}