小编Joh*_*ohn的帖子

如何解决索引超出范围错误?

我有一个包含studentList学生的组合框.当我选择学生时,它应该填写学生姓名的文本字段.每当从组合框中选择学生时,我都会收到以下错误

ArgumentOutOfRangeException was unhandled
Index was out of range. Must be non-negative and less than the size of the collection.
Run Code Online (Sandbox Code Playgroud)

我认为问题可能在我的循环中,但我无法找到如何解决错误,任何帮助将不胜感激

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    int i;
    for (i = 0; i < Main.studentList.Count; i++)
    {
        if (comboBox1.SelectedItem == Main.studentList[i].StudentName + " " + Main.studentList[i].StudentId)
        {                  
            break;
        }
    }

    txtName.Text = Main.studentList[i].StudentName; //where the error occurs
}

public void ChangeStudent_Load(object sender, EventArgs e)
{
    //loading combobox from studentList
    foreach (var student in Main.studentList)
    {
        comboBox1.Items.Add(student.StudentName + " …
Run Code Online (Sandbox Code Playgroud)

c#

4
推荐指数
2
解决办法
415
查看次数

如何正确使用共享首选项在Android中存储数据?

我正在制作一个计算班级人员BMI的应用程序,其中一个要求是我们使用共享首选项将计算的BMI发送到第二个活动.我的问题是,虽然我在运行应用程序时没有收到任何错误,但我认为没有任何错误发送.

这是我在启动时运行的主要活动.

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

    Button button = (Button)findViewById(R.id.btn1);

    final EditText Weight = (EditText)findViewById(R.id.txtWeight);
    final EditText Height = (EditText)findViewById(R.id.txtHeight);

    final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, Main2Activity.class));

        //the bmi calculation
           // bmi = (weight * 703) / (height*height);
            bmi = weight * height;


            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putInt("key1", weight);
            editor.putInt("key2", height);
            editor.putInt("key3", bmi);
            final boolean commit = editor.commit();

            }
        }
    );
}
Run Code Online (Sandbox Code Playgroud)

这是我的第二个活动,应该显示从第一个活动发送的BMI.

protected void onCreate(Bundle …
Run Code Online (Sandbox Code Playgroud)

android sharedpreferences

0
推荐指数
1
解决办法
83
查看次数

标签 统计

android ×1

c# ×1

sharedpreferences ×1