【问题标题】:Stored and clear array location存储和清除阵列位置
【发布时间】:2016-05-06 21:55:04
【问题描述】:

我在Select and选择复选框的情况下使用阵列位置中使用存储值的复选框表示清除特定数组索引值并在同一位置存储另一个复选框值。

public class MainActivity extends Activity {

int m[] =new int[5];
int count=1;
int t=0;
CheckBox chk1,chk2,chk3,chk4;
int flag,increment=0;
int b;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    chk1=(CheckBox)findViewById(R.id.checkBox1);
    chk2=(CheckBox)findViewById(R.id.checkBox2);
    chk3=(CheckBox)findViewById(R.id.checkBox3);
    chk4=(CheckBox)findViewById(R.id.checkBox4);
    Button next=(Button)findViewById(R.id.next);
    Button previous=(Button)findViewById(R.id.previous);      
    chk1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
            // TODO Auto-generated method stub

            if(chk1.isChecked())
            {                                       
                flag=1;
                m[increment]=flag;
                increment++;
                count++;                
            }
            else 
            { increment--;
            count--;        

            }               
        }
    });
    chk2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
            // TODO Auto-generated method stub
            if(chk2.isChecked())
            {
                flag=2;
                m[increment]=flag;
                increment++;
                count++;
            }
            else        
            {                   
                increment--;
                count--;
            }               
        }
    });
    chk3.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
            // TODO Auto-generated method stub

            if(chk3.isChecked())
            {
                flag=3;
                m[increment]=flag;
                increment++;
                count++;
            }
            else 
            {       
                increment--;
                count--;
            }
        }
    });
    chk4.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
            // TODO Auto-generated method stub
            if(chk1.isChecked())
            {
                flag=4;
                m[increment]=flag;
                increment++;
                count++;        
            }
            else
            {   
                increment--;
                count--;
            }
        }});
    next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            t++;
            if(t<4)
            {
                switch (t) {
                case 1:
                    b=m[0];
                    if(b==1)
                    {
                        Toast.makeText(getApplicationContext(), "one", 100).show();
                    }else if(b==2)
                    {
                        Toast.makeText(getApplicationContext(), "two", 100).show();
                    }else if(b==3)
                    {
                        Toast.makeText(getApplicationContext(), "three", 100).show();
                    }else if(b==4)
                    {
                        Toast.makeText(getApplicationContext(), "four", 100).show();
                    }
                    break;
                case 2:
                    b=m[1];
                    if(b==1)
                    {
                        Toast.makeText(getApplicationContext(), "one", 100).show();
                    }else if(b==2)
                    {
                        Toast.makeText(getApplicationContext(), "two", 100).show();
                    }else if(b==3)
                    {
                        Toast.makeText(getApplicationContext(), "three", 100).show();
                    }else if(b==4)
                    {
                        Toast.makeText(getApplicationContext(), "four", 100).show();
                    }
                    break;
                case 3:
                    b=m[2];
                    if(b==1)
                    {
                        Toast.makeText(getApplicationContext(), "one", 100).show();
                    }else if(b==2)
                    {
                        Toast.makeText(getApplicationContext(), "two", 100).show();
                    }else if(b==3)
                    {
                        Toast.makeText(getApplicationContext(), "three", 100).show();
                    }else if(b==4)
                    {
                        Toast.makeText(getApplicationContext(), "four", 100).show();
                    }
                    break;
                case 4:
                    b=m[3];
                    if(b==1)
                    {
                        Toast.makeText(getApplicationContext(), "one", 100).show();
                    }else if(b==2)
                    {
                        Toast.makeText(getApplicationContext(), "two", 100).show();
                    }else if(b==3)
                    {
                        Toast.makeText(getApplicationContext(), "three", 100).show();
                    }else if(b==4)
                    {
                        Toast.makeText(getApplicationContext(), "four", 100).show();
                    }
                    break;              
                default:
                    if(t<=3)
                    {
                        t=3;
                    }
                    break;
                }
            }
        }

    });
    previous.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            t--;
            if(t>=0)
            {
                switch (t) {
                case 1:
                    b=m[0];
                    if(b==1)
                    {
                        Toast.makeText(getApplicationContext(), "one", 100).show();
                    }else if(b==2)
                    {
                        Toast.makeText(getApplicationContext(), "two", 100).show();
                    }else if(b==3)
                    {
                        Toast.makeText(getApplicationContext(), "three", 100).show();
                    }else if(b==4)
                    {
                        Toast.makeText(getApplicationContext(), "four", 100).show();
                    }
                    break;
                case 2:
                    b=m[1];
                    if(b==1)
                    {
                        Toast.makeText(getApplicationContext(), "one", 100).show();
                    }else if(b==2)
                    {
                        Toast.makeText(getApplicationContext(), "two", 100).show();
                    }else if(b==3)
                    {
                        Toast.makeText(getApplicationContext(), "three", 100).show();
                    }else if(b==4)
                    {
                        Toast.makeText(getApplicationContext(), "four", 100).show();
                    }
                    break;
                case 3:
                    b=m[2];
                    if(b==1)
                    {
                        Toast.makeText(getApplicationContext(), "one", 100).show();
                    }else if(b==2)
                    {
                        Toast.makeText(getApplicationContext(), "two", 100).show();
                    }else if(b==3)
                    {
                        Toast.makeText(getApplicationContext(), "three", 100).show();
                    }else if(b==4)
                    {
                        Toast.makeText(getApplicationContext(), "four", 100).show();
                    }
                    break;
                case 4:
                    b=m[3];
                    if(b==1)
                    {
                        Toast.makeText(getApplicationContext(), "one", 100).show();
                    }else if(b==2)
                    {
                        Toast.makeText(getApplicationContext(), "two", 100).show();
                    }else if(b==3)
                    {
                        Toast.makeText(getApplicationContext(), "three", 100).show();
                    }else if(b==4)
                    {
                        Toast.makeText(getApplicationContext(), "four", 100).show();
                    }
                    break;              

                default:
                    if(t<=0)
                    {
                        t=1;
                    }
                    break;
                }
            }
        }

    });
}

这里是Logcat

E/AndroidRuntime(1522): FATAL EXCEPTION: main
    E/AndroidRuntime(1522): java.lang.ArrayIndexOutOfBoundsException: length=5index=-1

【问题讨论】:

  • 到底是什么问题?
  • java.lang.ArrayIndexOutOfBoundsException: length=5;索引=-1
  • 复选框是否默认选中?
  • 仅默认未选中!当检查标志应该增加并且在数组索引中的相同情况下会增加并取消选中的标志。 span>
  • 在下面查看我的答案。 chk4 的侦听器中应该有 chk4 而不是 chk1

标签: java android arrays checkbox


【解决方案1】:

我认为你的问题在这里:

chk4.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
        // TODO Auto-generated method stub
        if(chk1.isChecked())
        {
            flag=4;
            m[increment]=flag;
            increment++;
            count++;        
        }
        else
        {   
            increment--;
            count--;
        }
    }});

if 块中应该有 chk4 而不是 chk1。

【讨论】:

    【解决方案2】:

    您是否有可能在最后一个块中将“m”递增到其边界之外?

    chk4.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
        // TODO Auto-generated method stub
        if(chk1.isChecked())
        {
            flag=4;
            m[increment]=flag;
            increment++;
            count++;        
        }
        else
        {   
            increment--;
            count--;
        }
    }});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-24
      • 2014-08-16
      • 1970-01-01
      相关资源
      最近更新 更多