使用 for in android 显示数组字符串

display array string using for in android

我想在 android 中制作一个简单的测验应用程序,但我发现在显示我的问题时有些困难。我使用字符串数组来保留我的问题并尝试用数组显示它。 这是我的代码

这是我的错误日志

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.hikaru.sistempakar, PID: 2626 java.lang.ArrayIndexOutOfBoundsException: length=7; index=7 at com.example.hikaru.sistempakar.MainActivity.onClick(MainActivity.java:46)

public class MainActivity extends AppCompatActivity {
TextView tv;
Button btn;
RadioGroup rg;
RadioButton rb1,rb2;
String questions[]={"Kulit Memerah?","Kulit Gatal?","Kulit Bersisik?","Kulit Menggumpal?","Kulit Melemouble?","Kulit Menebal?","Iritasi Kulit?"};
String opt[]={"Ya","Tidak"};
String ans[]={};

int flag=0;
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv=(TextView)findViewById(R.id.textView);
    rb1=(RadioButton)findViewById(R.id.radioButton1);
    rb2=(RadioButton)findViewById(R.id.radioButton2);
    btn=(Button)findViewById(R.id.button);
    rg=(RadioGroup)findViewById(R.id.radioGroup1);

    tv.setText(questions[flag]);
    rb1.setText(opt[0]);
    rb2.setText(opt[1]);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // RadioButton uans=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
            //String ansText=uans.getText().toString();

            for(flag=0;flag<=questions.length;flag++)
            {
                tv.setText(questions[flag]);
                rb1.setText(opt[0]);
                rb2.setText(opt[1]);

            }
            //Intent in = new Intent(getApplicationContext(),ResultActivity.class);
            //startActivity(in);

        }
    });
}

}

当您的标志值达到 7 时,它会检查条件 flag<=questions.length,这将 return 为真,但您的数组中实际上没有第 7 个项目。这就是您出错的原因。

将您的代码更改为:

for(flag=0;flag<questions.length;flag++)
            {
                tv.setText(questions[flag]);
                rb1.setText(opt[0]);
                rb2.setText(opt[1]);

            }

for(flag=1;flag<=questions.length;flag++)
            {
                tv.setText(questions[flag]);
                rb1.setText(opt[0]);
                rb2.setText(opt[1]);

            }

使用以下代码。这将在点击按钮时继续循环你的问题。

public class MainActivity extends AppCompatActivity {
    TextView tv;
    Button btn;
    RadioGroup rg;
    RadioButton rb1,rb2;
    String questions[]={"Kulit Memerah?","Kulit Gatal?","Kulit Bersisik?","Kulit Menggumpal?","Kulit Melemouble?","Kulit Menebal?","Iritasi Kulit?"};
    String opt[]={"Ya","Tidak"};
    String ans[]={};

    int flag=0;
    int i=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv=(TextView)findViewById(R.id.textView);
        rb1=(RadioButton)findViewById(R.id.radioButton1);
        rb2=(RadioButton)findViewById(R.id.radioButton2);
        btn=(Button)findViewById(R.id.button);
        rg=(RadioGroup)findViewById(R.id.radioGroup1);

        tv.setText(questions[flag]);
        rb1.setText(opt[0]);
        rb2.setText(opt[1]);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flag++;
                if(flag >= questions.length()){
                    flag=0;
                }
                tv.setText(questions[flag]);
            }
        });
    }
}