【问题标题】:display array string using for in android在android中使用for显示数组字符串
【发布时间】:2016-02-28 06:40:08
【问题描述】:

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

这是我的错误日志

E/AndroidRuntime: 致命异常: main 进程:com.example.hikaru.sistempakar,PID:2626 java.lang.ArrayIndexOutOfBoundsException:长度=7;索引=7 在 com.example.hikaru.sistempakar.MainActivity$1.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);

        }
    });
}

}

【问题讨论】:

  • 有什么问题?
  • 当我运行它时,仅显示数组索引 0 中的项目的项目然后应用程序被强制关闭
  • E/AndroidRuntime: 致命异常: 主进程: com.example.hikaru.sistempakar, PID: 2626 java.lang.ArrayIndexOutOfBoundsException: length=7; index=7 在 com.example.hikaru.sistempakar.MainActivity$1.onClick(MainActivity.java:46)
  • 将循环条件更改为flag&lt;questions.length,删除=
  • 还是不行,我之前试过... ._.a

标签: android arrays for-loop display


【解决方案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]);
            }
        });
    }
}

【讨论】:

    【解决方案2】:

    当您的标志值达到 7 时,它会检查条件 flag&lt;=questions.length,该条件将返回 true,但实际上您的数组中没有第 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]);
    
                }
    

    【讨论】:

    • 第二个选项仍然会抛出 java.lang.ArrayIndexOutOfBoundsException
    • 谢谢..它没有强制关闭,但只是索引 0 和最后一个索引可以显示,索引 1 - 6 不能显示...是什么原因?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 2019-09-09
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多