2016-12-26 4 views
-1

ボタンを押したときにコードに問題があります。イメージビューを1つのイメージに変更したいのですが、問題が1つしかありません。 。クリックビューエラー時のImageViewの変更

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

エラーあなたの私の変数が配列の範囲外であることを述べています。チェックを追加して、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--]); 

     } 
    } 
+0

ハイ仲間のTYそんなにその作業が、私は彼がラスのPICに移動してくださいafther問題があります。 –

+0

こんにちは仲間のTYそんなにその作業をしかし、私は彼がラスのPICに行くafther問題があり、彼は停止することはありませんどのように私はどのようにして、ユーザーは行くと変更する必要がありますどのくらいを変更しますか?あなたの時間のために多くの時間を過ごしてください –

関連する問題