ImageView更改单击错误

时间:2016-12-26 21:59:26

标签: java android arrays

我的代码有问题,当我按下按钮时,我希望图像视图更改为1个图像,问题是他只更改1张图片并停止工作,我尝试而不是。
第二个问题,当我按下第二个按钮时,应用程序崩溃,这是日志:

E/AndroidRuntime: FATAL EXCEPTION: main
              java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
                  at com.example.hanansanag.mytourneyccreator.Players.onClick(Players.java:47)
                  at android.view.View.performClick(View.java:4240)
                  at android.view.View$PerformClick.run(View.java:17721)
                  at android.os.Handler.handleCallback(Handler.java:730)
                  at android.os.Handler.dispatchMessage(Handler.java:92)
                  at android.os.Looper.loop(Looper.java:137)
                  at android.app.ActivityThread.main(ActivityThread.java:5103)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:525)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                  at dalvik.system.NativeStart.main(Native Method)

代码粉碎:

  else   if (btnBeck==v) {
        iv.setBackgroundResource(pic[--i]);

}

和下一个pic按钮问题代码类:

    package com.example.hanansanag.mytourneyccreator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;

/**
 * Created by ssh on 25/12/2016.
 */

public class Players extends AppCompatActivity implements View.OnClickListener {
    protected Button btnNext, btnBeck;
    protected ImageView iv;
    protected String fname;
    protected String Lname;
    protected String team;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.team_pic);
        btnNext = (Button) findViewById(R.id.btnNextPic);
        btnBeck = (Button) findViewById(R.id.btnBeckPic);
        iv = (ImageView) findViewById(R.id.imageView);

        btnNext.setOnClickListener(this);
        btnBeck.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        int i=0;

        int[] pic = {R.drawable.barcelona, R.drawable.athletico, R.drawable.arsenal1, R.drawable.chelsea,
                R.drawable.dortmond, R.drawable.bayrenminchen, R.drawable.inter, R.drawable.realmadrid,
                R.drawable.macabiheifa, R.drawable.macabitelaviv, R.drawable.beitar, R.drawable.bersheva};
        if (btnNext==v) {
                iv.setBackgroundResource(pic[++i]);

        }
      else   if (btnBeck==v) {
                iv.setBackgroundResource(pic[--i]);

        }
    }
}

xml文件:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/teampic"
    android:orientation="horizontal" android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/gridLayout"/>


    <TextView
        android:text="Fname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/imageView"
        android:layout_marginTop="12dp"
        android:id="@+id/textView2" />

    <TextView
        android:text="Lname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:id="@+id/Lname"
        android:layout_below="@+id/textView2"
        android:layout_toEndOf="@+id/imageView" />

    <TextView
        android:text="Fname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/imageView"
        android:layout_marginTop="12dp"
        android:id="@+id/textView3" />

    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_toEndOf="@+id/textView2">

    <Button
        android:layout_width="20dp"
        android:layout_height="15dp"
        android:id="@+id/btnBeckPic"
        android:background="@drawable/btnbeck"
        android:layout_marginStart="30dp"
        android:layout_alignBottom="@+id/Lname"
        android:layout_toEndOf="@+id/textView2" />

    <Button
        android:background="@drawable/btnnext"
        android:layout_width="20dp"
        android:layout_height="15dp"
        android:id="@+id/btnNextPic"

        android:layout_below="@+id/imageView"
        android:layout_toEndOf="@+id/btnBeckPic"
        android:layout_marginStart="14dp" />
    </GridLayout>

    <ImageView
        android:layout_height="50dp"
        android:id="@+id/imageView"
        android:layout_width="60dp"
        android:layout_below="@+id/gridLayout"
        android:layout_toEndOf="@+id/gridLayout"/>
</RelativeLayout>

manefist如果需要:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.hanansanag.mytourneyccreator">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".OpeningScreen">


            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>

        </activity>
        <activity android:name=".Players"></activity>
        <activity android:name=".MainScreen"></activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

错误表示你的i变量超出了数组范围。尝试添加检查,并从onclick移动初始化代码。例如:

int i=0;

        int[] pic = {R.drawable.barcelona, R.drawable.athletico, R.drawable.arsenal1, R.drawable.chelsea,
                R.drawable.dortmond, R.drawable.bayrenminchen, R.drawable.inter, R.drawable.realmadrid,
                R.drawable.macabiheifa, R.drawable.macabitelaviv, R.drawable.beitar, R.drawable.bersheva};

@Override
    public void onClick(View v) {
        if(i <= 0 || i >= pic.length){
             return;
        }

        if (btnNext==v) {
                iv.setBackgroundResource(pic[i++]);

        }
      else   if (btnBeck==v) {
                iv.setBackgroundResource(pic[i--]);

        }
    }