应用程序在数组末尾崩溃

时间:2015-12-28 17:55:23

标签: android arrays

我的应用程序在蓝色堆栈中的数组末尾崩溃。我不知道为什么。

当我点击数组末尾的下一个按钮时,应用程序崩溃了。我也在手机上测试了它,结果相同。应用程序的其余部分按预期运行。

据我所知“i%= image_elements.length;”应该是循环数组的函数。

我很确定这是崩溃的来源。

i++;

element.setImageResource(image_elements[i]);
name.setImageResource(image_names[i]);

i %= image_elements.length;

以下完整代码

public class Practice extends MainMenuActivity {

    int i = 0;
    final int[] image_elements = {
        R.drawable.spr_elements_0,
        R.drawable.spr_elements_1,
        [...]
        R.drawable.spr_elements_86,
        R.drawable.spr_elements_87,
    };
    final int[] image_names = {
        R.drawable.spr_name_0,
        R.drawable.spr_name_1,
        [...]
        R.drawable.spr_name_86,
        R.drawable.spr_name_87,
    };

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.practice);

            final ImageView element = (ImageView) findViewById(R.id.element);
            final ImageView name = (ImageView) findViewById(R.id.name);

            Button nextButton = (Button) findViewById(R.id.buttonNext);
            nextButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                            i++;

                            element.setImageResource(image_elements[i]);
                            name.setImageResource(image_names[i]);

                            i %= image_elements.length;
                    }
            });
    }
    public void backButton(View view) {
            Intent z = new Intent(this, MainMenuActivity.class);
            startActivity(z);
    }
}

1 个答案:

答案 0 :(得分:1)

您需要重新安排代码:

                        i++;

                        element.setImageResource(image_elements[i]);
                        name.setImageResource(image_names[i]);

                        i %= image_elements.length;

到此:

                        i++;
                        i %= image_elements.length;

                        element.setImageResource(image_elements[i]);
                        name.setImageResource(image_names[i]);

否则会发生索引增加超出数组边界的情况,然后使用模数运算符进行校正。在使用索引之前,您需要进行更正

在这种特殊情况下,

i %= image_elements.length

基本相同
if( i == image_elements.length ) {
    i = 0;
}

数组索引从0length-1

您可以通过按名称查找资源来完全摆脱数组,例如:

final static int MAX_ELEMENTS = 88; // this includes 0..87
private int index = 0;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.practice);

        final ImageView element = (ImageView) findViewById(R.id.element);
        final ImageView name = (ImageView) findViewById(R.id.name);
        final Resources res = this.getResources();
        final String pkgName = this.getPackageName();

        Button nextButton = (Button) findViewById(R.id.buttonNext);
        nextButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                        final int imgId = res.getIdentifier( "spr_elements_" + index, "drawable", pkgName );
                        final int nameId = res.getIdentifier( "spr_name_" + index, "drawable", pkgName );
                        element.setImageResource( imgId );
                        name.setImageResource( nameId );

                        index = (index+1) % MAX_ELEMENTS;
                }
        });
}