上一个按钮不起作用

时间:2013-11-21 19:33:04

标签: android button

我有一个只使用ImageView切换屏幕的应用。我有两个按钮:上一个和下一个。这些按钮可浏览ImageView布局中的图像。我的下一步按钮工作得很好,但上一个按钮根本不起作用。它不会回到上一张图片。这是我的整个活动:

public class StanActivity extends Activity {

    ImageView image;
    Button next;
    int a = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stan_xml);

        image = (ImageView) findViewById(R.id.imageView1);

        next = (Button) findViewById(R.id.next);
        next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
                switch (view.getId()) {

                case R.id.next:
                    if (a == 0) {
                        image.setImageResource(R.drawable.stan1);
                        a = 1;
                    } else if (a == 1) {
                        image.setImageResource(R.drawable.stan2);
                        a = 2;
                    } else if (a == 2) {
                        image.setImageResource(R.drawable.stan3);
                        a = 3;
                    } else if (a == 3) {
                        image.setImageResource(R.drawable.stan4);
                        a = 4;
                    } else if (a == 4) {
                        image.setImageResource(R.drawable.stan5);
                        a = 5;
                    } else if (a == 5) {
                        image.setImageResource(R.drawable.stan6);
                        a = 6;
                    } else if (a == 6) {
                        image.setImageResource(R.drawable.stan7);
                        a = 7;
                    } else if (a == 7) {
                        image.setImageResource(R.drawable.stan8);
                        a = 8;
                    } else if (a == 8) {
                        image.setImageResource(R.drawable.finalstan);
                        a = 9;
                        next.setVisibility(View.GONE);
                    }
                }
            }
        });

        Button previous = (Button) findViewById(R.id.prev);
        previous.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
                switch (view.getId()) {
                case R.id.prev:
                    a--;
                    next.performClick();
                    break;
                }
            }
        });

    }
}

感谢您的帮助。

更新代码:

    public class StanActivity extends Activity {

    public int currentImage = 0;

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

        final int[] images = new int[] { R.drawable.stan1, R.drawable.stan2,
                R.drawable.stan3, R.drawable.stan4, R.drawable.stan5,
                R.drawable.stan6, R.drawable.stan7, R.drawable.stan8 };

        final ImageView image = (ImageView) findViewById(R.id.imageView1);
        final Button next = (Button) findViewById(R.id.next);
        final Button previous = (Button) findViewById(R.id.prev);

        next.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                currentImage++;
                if (currentImage >= images.length) {
                    currentImage = images.length;
                    next.setVisibility(View.GONE);
                }
                previous.setVisibility(View.VISIBLE);
                image.setImageResource(images[currentImage]); /* ERROR IN THIS LINE  */
            }
        });

        previous.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                currentImage--;
                if (currentImage <= 0) {
                    currentImage = 0;
                    previous.setVisibility(View.GONE);
                }
                next.setVisibility(View.VISIBLE);
                image.setImageResource(images[currentImage]);
            }
        });
    }
}

和LogCat:

11-21 22:16:18.101: D/dalvikvm(22524): GC_FOR_ALLOC freed 3193K, 12% free 61564K/69760K, paused 34ms, total 34ms
11-21 22:16:18.611: D/dalvikvm(22524): GC_FOR_ALLOC freed 3032K, 15% free 61512K/71660K, paused 21ms, total 21ms
11-21 22:16:18.611: I/dalvikvm-heap(22524): Grow heap (frag case) to 67.738MB for 2492416-byte allocation
11-21 22:16:19.341: D/dalvikvm(22524): GC_FOR_ALLOC freed 2979K, 17% free 61955K/74096K, paused 13ms, total 13ms
11-21 22:16:20.191: D/AndroidRuntime(22524): Shutting down VM
11-21 22:16:20.191: W/dalvikvm(22524): threadid=1: thread exiting with uncaught exception (group=0x417c5898)
11-21 22:16:20.191: E/AndroidRuntime(22524): FATAL EXCEPTION: main
11-21 22:16:20.191: E/AndroidRuntime(22524): java.lang.ArrayIndexOutOfBoundsException: length=8; index=8
11-21 22:16:20.191: E/AndroidRuntime(22524):    at deimantasb.howtodrawsouthpark.StanActivity$1.onClick(StanActivity.java:37)
11-21 22:16:20.191: E/AndroidRuntime(22524):    at android.view.View.performClick(View.java:4475)
11-21 22:16:20.191: E/AndroidRuntime(22524):    at android.view.View$PerformClick.run(View.java:18784)
11-21 22:16:20.191: E/AndroidRuntime(22524):    at android.os.Handler.handleCallback(Handler.java:730)
11-21 22:16:20.191: E/AndroidRuntime(22524):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-21 22:16:20.191: E/AndroidRuntime(22524):    at android.os.Looper.loop(Looper.java:137)
11-21 22:16:20.191: E/AndroidRuntime(22524):    at android.app.ActivityThread.main(ActivityThread.java:5414)
11-21 22:16:20.191: E/AndroidRuntime(22524):    at java.lang.reflect.Method.invokeNative(Native Method)
11-21 22:16:20.191: E/AndroidRuntime(22524):    at java.lang.reflect.Method.invoke(Method.java:525)
11-21 22:16:20.191: E/AndroidRuntime(22524):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
11-21 22:16:20.191: E/AndroidRuntime(22524):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
11-21 22:16:20.191: E/AndroidRuntime(22524):    at dalvik.system.NativeStart.main(Native Method)
11-21 22:21:30.321: I/Process(22524): Sending signal. PID: 22524 SIG: 9

2 个答案:

答案 0 :(得分:1)

您之前的按钮正在点击下一个按钮,这意味着它正在执行a = a - 1 + 1 = a。解决这个问题的一种愚蠢的方法是让前一个按钮返回两个(a - = 2;),然后点击下一个。

将所有资源放入数组可能更好: int [] images = new int [] {R.drawable.stan1,R.drawable.stan2,...}; 然后使用image.setImageResource(images [a]);代替。

试试这个:

public class StanActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stan_xml);

        long[] images = new long[] {R.drawable.stan1, R.drawable.stan2, R.drawable.stan3 /* etc */};
        int currentImage = 0;

        ImageView image = (ImageView) findViewById(R.id.imageView1);
        Button next = (Button) findViewById(R.id.next);
        Button previous = (Button) findViewById(R.id.prev);

        next.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                currentImage++;
                if (currentImage >= images.length - 1) {
                    currentImage = images.length - 1;
                    next.setVisibility(View.GONE);
                }
                previous.setVisibility(View.VISIBLE);
                image.setImageResource(images[currentImage]);
            }
        });

        previous.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                currentImage--;
                if (currentImage <= 0) {
                    currentImage = 0;
                    previous.setVisibility(VIEW.GONE);
                }
                next.setVisibility(View.VISIBLE);
                image.setImageResource(images[currentImage]);
            }
        });      
    }      
}          

答案 1 :(得分:0)

使用a = a-2代替a--。或者像你下一次做的那样实现上一个按钮。